To connect to the Configuration Manager client Windows Management Instrumentation (WMI) provider, you create a ManagementScope object in the \\Client\root\ccm namespace.
You use the ManagementScope object to read and query WMI objects. For example, How to Read a WMI Object by Using System.Management.
To connect to the Configuration Manager client WMI provider
-
In Visual Studio, create a new Visual C# Console Project.
-
Add a reference to the System.Management assembly.
-
In the C# source code, add a reference to the System.Management namespace with the following code.
-
using System.Management;
-
Create a new class and add the following connection example code.
Example
The following C# code example creates and returns a ManagementScope object on the root\ccm namespace.
For information about calling the sample code, see How to Call a WMI Class Method by Using System.Management.
C# | Copy Code |
---|---|
public ManagementScope Connect() { try { return new ManagementScope(@"root\ccm"); } catch (System.Management.ManagementException e) { Console.WriteLine("Failed to connect", e.Message); throw; } } |
Compiling the Code
Namespaces
System
System.Management
Assembly
System.Management.dll
Robust Programming
The exception that can be raised is System.Management.ManagementException.