Bewerken

Share via


ManagementClass.CreateInstance Method

Definition

Initializes a new instance of the WMI class.

public:
 System::Management::ManagementObject ^ CreateInstance();
public System.Management.ManagementObject CreateInstance ();
member this.CreateInstance : unit -> System.Management.ManagementObject
Public Function CreateInstance () As ManagementObject

Returns

A ManagementObject that represents a new instance of the WMI class.

Examples

The following example shows how to initialize a ManagementClass variable with a ManagementClass constructor and then create a new instance of a WMI class.

using System;
using System.Management;

public class Sample
{
    public static void Main()
    {

        ManagementClass envClass =
            new ManagementClass("Win32_Environment");
        ManagementObject newInstance =
            envClass.CreateInstance();
        newInstance["Name"] = "testEnvironmentVariable";
        newInstance["VariableValue"] = "testValue";
        newInstance["UserName"] = "<SYSTEM>";
        newInstance.Put(); //to commit the new instance.
    }
}
Imports System.Management


Class Sample
    Public Overloads Shared Function _
        Main(ByVal args() As String) As Integer

        Dim envClass As New ManagementClass( _
            "Win32_Environment")
        Dim newInstance As ManagementObject

        newInstance = envClass.CreateInstance()
        newInstance("Name") = "testEnvironmentVariable"
        newInstance("VariableValue") = "testValue"
        newInstance("UserName") = "<SYSTEM>"
        newInstance.Put()  'to commit the new instance.

    End Function
End Class

Remarks

Note that the new instance is not committed until the Put() method is called. Before committing it, the key properties must be specified.

.NET Framework Security

Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.

Applies to