Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Exploring WMI: Integrate WMI into the .NET Framework : Page 3

Once you learn how to integrate Windows Management Instrumentation (WMI) with the .NET Framework, you can utilize a completely new approach for managing system resources.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Coding the Application Logic
After finishing your ServiceNode class, continue with the code for the application logic. Because your sample is simple, you can add this code directly to the form class.

First off, add the necessary namespace declaration to the header of your class file:

using System.Management;

Now, create a method RefreshServiceList, which will be called in the constructor every time a user selects the Refresh menu item:

void RefreshServiceList() { lvServices.Items.Clear(); ManagementClass mc = new ManagementClass("Win32_Service"); foreach(ManagementObject mo in mc.GetInstances()) lvServices.Items.Add(new ServiceNode(mo)); foreach(ToolBarButton b in ServiceToolBar.Buttons) b.Enabled = false; }

This method clears the Items collection of your ListView lvServices. Then it creates a new object of the type ManagementClass, which takes the name of the CIMV2 class as a constructor parameter. Since you want windows services, take 'Win32_Service'.

To access the individual services that are installed on your machine, you need to call the method GetInstances() on your ManagementClass object. This call returns an object of type ManagementObjectCollection, which you can iterate with a foreach loop to access the ManagementObject instances that are contained with the collection. Each instance of a service is now used with the constructor of your ServiceNode class to create a new instance of a ServiceNode, which is then added to the Items collection of your ListView lvServices.

The second foreach loop in this method is used to disable all buttons on the toolbar because the clearing of the Items collection in the ListView also cleared the SelectedItem, and without any item selected, no action could be performed.

Now you can add the call to RefreshServiceList to the constructor of your Form:

public ServiceForm() { InitializeComponent(); RefreshServiceList(); }

To create the event handler for the SelectedItemChanged event of your ListView, simply double click on the ListView in the form designer. You will add application logic to enable and disable the toolbar buttons according to the features of your services:

private void lvServices_SelectedIndexChanged(object sender, System.EventArgs e) { if(lvServices.SelectedItems.Count!=1) { foreach(ToolBarButton tbb in ServiceToolBar.Controls) tbb.Enabled = false; } else { ServiceNode sn = (ServiceNode)lvServices.SelectedItems[0]; switch(sn.State) { case "Running": tbbStart.Enabled = false; tbbStop.Enabled = true; tbbRestart.Enabled = true; tbbResume.Enabled = false; tbbPause.Enabled = true; break; case "Stopped": tbbStart.Enabled = true; tbbStop.Enabled = false; tbbRestart.Enabled = false; tbbResume.Enabled = false; tbbPause.Enabled = false; break; case "Paused": tbbStart.Enabled = false; tbbStop.Enabled = true; tbbRestart.Enabled = true; tbbResume.Enabled = true; tbbPause.Enabled = false; break; default: foreach(ToolBarButton tbb in ServiceToolBar.Controls) tbb.Enabled = false; break; } if(!sn.CanPause) tbbResume.Enabled = tbbPause.Enabled = false; } }

If no service is selected in your ListView, you simply disable all your toolbar buttons. If a service is selected, you need to differentiate the state of the Service. Create a switch statement on the property ServiceNode.State. Depending on the state of the server, you either enable or disable the toolbar buttons. In the case of no valid state or an unknown state, simply disable all buttons. Last but not least, check whether the service cannot pause and disable the Resume and Pause buttons.

Next, implement the event handlers for your menu items. Just double click on the menu item Refresh and then Exit. The implementation of these two event handlers is straightforward:

private void miExit_Click(object sender, System.EventArgs e) { this.Close(); } private void miRefresh_Click(object sender, System.EventArgs e) { RefreshServiceList(); }

When the user clicks on the Exit menu item, you simply shut your application down by calling the Close() method of your form. When the user clicks on Refresh, you rebuild your ListView with a call on the RefreshServiceList() method.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date