共用方式為


How to Connect to the Configuration Manager Client WMI Namespace by Using System.Management

Applies To: System Center Configuration Manager 2007, System Center Configuration Manager 2007 R2, System Center Configuration Manager 2007 R3, System Center Configuration Manager 2007 SP1, System Center Configuration Manager 2007 SP2

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

  1. In Visual Studio, create a new Visual C# Console Project.

  2. Add a reference to the System.Management assembly.

  3. In the C# source code, add a reference to the System.Management namespace with the following code.

  4. using System.Management;

  5. 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.

public ManagementScope Connect()
{
    try
    {
        return new ManagementScope(@"root\ccm");
    }
    catch (System.Management.ManagementException e)
    {
        Console.WriteLine("Failed to connect", e.Message);
        throw;
    }
}

This example method has the following parameters:

Parameter Type Description

Connection

WqlConnectionManager

A valid connection to the SMS Provider.

Compiling the Code

Namespaces

System.

System.Management.

Assembly

System.Management.

Robust Programming

The exception that can be raised is System.Management.ManagementException.

See Also

Concepts

About Configuration Manager WMI Programming
How to Call a WMI Class Method by Using System.Management
How to Perform an Asynchronous Query by Using System.Management
How to Perform a Synchronous Query by Using System.Management
How to Read a WMI Object by Using System.Management