I have this Windows Form Application that has a listview of services in Win_32. In my application I am trying to implement a refresh button.
However while debugging I keep getting this NullReferenceException error.
I did notice that when I hovered over this part of my code
foreach (ManagementObject moService in mcServices.GetInstances())
I get a notification that says this "ManagementObjectCollection ManagementClass.GetInstances()(+3 overload(s))"
Here is the my full code
public void loadListBox()
{
foreach (ListViewItem svc in listView1.Items)
{
listView1.Items.Remove(svc);
}
ManagementClass mcServices = new ManagementClass("Win32_Service");
foreach (ManagementObject moService in mcServices.GetInstances())
{
ListViewItem newItem = new ListViewItem(moService.GetPropertyValue("Caption").ToString());
newItem.SubItems.Add(moService.GetPropertyValue("Description").ToString());
if (moService.GetPropertyValue("State").ToString().Equals("Running"))
{
newItem.SubItems.Add(moService.GetPropertyValue("State").ToString());
}
else
{
newItem.SubItems.Add("");
}
newItem.SubItems.Add(moService.GetPropertyValue("StartMode").ToString());
newItem.SubItems.Add(moService.GetPropertyValue("StartName").ToString());
listView1.Items.Add(newItem);
}
}
private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
loadListBox();
}
The exception occurs here
newItem.SubItems.Add(moService.GetPropertyValue("Description").ToString());
Please help

New Topic/Question
Reply



MultiQuote




|