Introduction

This sample shows how to use Windows Management Instrumentation (WMI) with C#. This uses the System.Management assembly. It lists the services from a machine. It shows how to get a list of services on a named computer

Building the Sample

There are no special requirements to build this sample. It uses .NET 4.0

Description

This sample demonstrates how to use WMI with C#. 

The CreateNewManagementScope function shows how to create a new ManagementScope object which allows you to specify different credentials and options when making the connection to remote machines. The ConnectionOptions are not required for local connections

C#
Edit|Remove
private ManagementScope CreateNewManagementScope(string server) 
        { 
            string serverString = @"\\" + server + @"\root\cimv2"; 
 
            ManagementScope scope = new ManagementScope(serverString); 
 
            if (!chkUseCurrentUser.Checked) 
            { 
                ConnectionOptions options = new ConnectionOptions 
                                  { 
                                      Username = txtUsername.Text, 
                                      Password = txtPassword.Text, 
                                      Impersonation = ImpersonationLevel.Impersonate, 
                                      Authentication = AuthenticationLevel.PacketPrivacy 
                                  }; 
                scope.Options = options; 
            } 
 
            return scope; 
        }

The function GetServicesForComputer uses a WMI Select query run by the ManagementObjectSearcher with the ManagementScope to generate a list of ManagementObjects which are in this case Windows Services.

From there we use LINQ to generate a generic list of strings which are bound to the ListBox on the form

C#
Edit|Remove
 private void GetServicesForComputer(string computerName) 
        { 
            ManagementScope scope = CreateNewManagementScope(computerName); 
 
            SelectQuery query = new SelectQuery("select * from Win32_Service"); 
 
            try 
            { 
                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query)) 
                { 
                    ManagementObjectCollection services = searcher.Get(); 
 
                    List<string> serviceNames = 
                        (from ManagementObject service in services select service["Caption"].ToString()).ToList(); 
 
                    lstServices.DataSource = serviceNames; 
                } 
            } 
            catch (Exception exception) 
            { 
                lstServices.DataSource = null; 
                lstServices.Items.Clear(); 
                lblErrors.Text = exception.Message; 
                Console.WriteLine(Resources.MainForm_GetServicesForServer_Error__ + exception.Message); 
            } 
        }
 
More Information

For more information on the different objects used in this sample and the Windows Management namespace please check the following links