ManagementObject 建構函式

定義

初始化 ManagementObject 類別的新執行個體。Initializes a new instance of the ManagementObject class.

多載

ManagementObject()

初始化 ManagementObject 類別的新執行個體。Initializes a new instance of the ManagementObject class. 這是無參數建構函式。This is the parameterless constructor.

ManagementObject(ManagementPath)

初始化指定 WMI 物件路徑的 ManagementObject 類別的新執行個體。Initializes a new instance of the ManagementObject class for the specified WMI object path. 路徑會提供做為 ManagementPathThe path is provided as a ManagementPath.

ManagementObject(String)

初始化指定 WMI 物件路徑的 ManagementObject 類別的新執行個體。Initializes a new instance of the ManagementObject class for the specified WMI object path. 路徑會提供為字串。The path is provided as a string.

ManagementObject(ManagementPath, ObjectGetOptions)

初始化繫結到指定的 WMI 路徑 (包括指定的其他選項) 之 ManagementObject 類別的新執行個體。Initializes a new instance of the ManagementObject class bound to the specified WMI path, including the specified additional options.

ManagementObject(SerializationInfo, StreamingContext)

初始化可序列化之 ManagementObject 類別的新執行個體。Initializes a new instance of the ManagementObject class that is serializable.

ManagementObject(String, ObjectGetOptions)

初始化繫結到指定的 WMI 路徑 (包括指定的其他選項) 之 ManagementObject 類別的新執行個體。Initializes a new instance of the ManagementObject class bound to the specified WMI path, including the specified additional options. 在這項變數中,路徑可以指定為字串。In this variant, the path can be specified as a string.

ManagementObject(ManagementScope, ManagementPath, ObjectGetOptions)

初始化繫結至指定的 WMI 路徑 (包括指定的選項) 之 ManagementObject 類別的新執行個體。Initializes a new instance of the ManagementObject class bound to the specified WMI path that includes the specified options.

ManagementObject(String, String, ObjectGetOptions)

初始化繫結至指定的 WMI 路徑 (且包括指定的選項) 之 ManagementObject 類別的新執行個體。Initializes a new instance of the ManagementObject class bound to the specified WMI path, and includes the specified options. 範圍和路徑會指定為字串。The scope and the path are specified as strings.

ManagementObject()

初始化 ManagementObject 類別的新執行個體。Initializes a new instance of the ManagementObject class. 這是無參數建構函式。This is the parameterless constructor.

public:
 ManagementObject();
public ManagementObject ();
Public Sub New ()

範例

下列範例會 ManagementObject 使用無參數的函式,初始化類別的新實例。The following example initializes a new instance of the ManagementObject class with the parameterless constructor.

using System;
using System.Management;

class Sample
{
    public static int Main(string[] args)
    {
        ManagementObject o = new ManagementObject();

        // Now set the path on this object to
        // bind it to a 'real' manageable entity
        o.Path =
            new ManagementPath("Win32_LogicalDisk='c:'");

        //Now it can be used
        Console.WriteLine(o["FreeSpace"]);

        return 0;
    }
}
Imports System.Management

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

        Dim o As New ManagementObject

        Dim mp As New _
            ManagementPath("Win32_LogicalDisk='c:'")

        ' Now set the path on this object to
        ' bind it to a 'real' manageable entity
        o.Path = mp

        'Now it can be used 
        Console.WriteLine(o("FreeSpace"))

        Return 0
    End Function
End Class

備註

.NET Framework 安全性.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.

適用於

ManagementObject(ManagementPath)

初始化指定 WMI 物件路徑的 ManagementObject 類別的新執行個體。Initializes a new instance of the ManagementObject class for the specified WMI object path. 路徑會提供做為 ManagementPathThe path is provided as a ManagementPath.

public:
 ManagementObject(System::Management::ManagementPath ^ path);
public ManagementObject (System.Management.ManagementPath path);
new System.Management.ManagementObject : System.Management.ManagementPath -> System.Management.ManagementObject
Public Sub New (path As ManagementPath)

參數

path
ManagementPath

ManagementPath,含有通往 WMI 物件的路徑。A ManagementPath that contains a path to a WMI object.

範例

下列範例會 ManagementObject 使用指定的 WMI 物件路徑,初始化類別的新實例。The following example initializes a new instance of the ManagementObject class with a specified WMI object path.

using System;
using System.Management;

class Sample
{
    public static int Main(string[] args)
    {
        ManagementPath p =
            new ManagementPath(
            "Win32_Service.Name='Alerter'");
        ManagementObject o = new ManagementObject(p);

        //Now it can be used
        Console.WriteLine(o["Name"]);

        return 0;
    }
}
Imports System.Management

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

        Dim p As New ManagementPath( _
            "Win32_Service.Name=""Alerter""")
        Dim o As New ManagementObject(p)

        'Now it can be used 
        Console.WriteLine(o("Name"))

        Return 0
    End Function
End Class

備註

.NET Framework 安全性.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.

適用於

ManagementObject(String)

初始化指定 WMI 物件路徑的 ManagementObject 類別的新執行個體。Initializes a new instance of the ManagementObject class for the specified WMI object path. 路徑會提供為字串。The path is provided as a string.

public:
 ManagementObject(System::String ^ path);
public ManagementObject (string path);
new System.Management.ManagementObject : string -> System.Management.ManagementObject
Public Sub New (path As String)

參數

path
String

WMI 路徑。A WMI path.

範例

下列範例會初始化類別的新實例 ManagementObjectThe following example initializes a new instance of the ManagementObject class.

using System;
using System.Management;

class Sample
{
    public static int Main(string[] args)
    {
        ManagementObject o =
            new ManagementObject("Win32_Service.Name='Alerter'");

        //or with a full path :

        ManagementObject mObj =
            new ManagementObject(
            "\\\\MyServer\\root\\MyApp:MyClass.Key='abc'");

        return 0;
    }
}
Imports System.Management

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

        Dim o As New ManagementObject( _
    "Win32_Service.Name=""Alerter""")

        ' or with a full path :

        Dim mObj As New ManagementObject( _
            "\\\\MyServer\\root\\MyApp:MyClass.Key=""abc""")

        Return 0
    End Function
End Class

備註

如果指定的路徑為相對路徑,但未指定 (伺服器或命名空間) ,預設路徑是本機電腦,預設命名空間則是預設的 DefaultPath 路徑 (root\cimv2) 。If the specified path is a relative path only (a server or namespace is not specified), the default path is the local machine, and the default namespace is the DefaultPath path (by default, root\cimv2). 如果使用者指定完整路徑,則會覆寫預設設定。If the user specifies a full path, the default settings are overridden.

.NET Framework 安全性.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.

適用於

ManagementObject(ManagementPath, ObjectGetOptions)

初始化繫結到指定的 WMI 路徑 (包括指定的其他選項) 之 ManagementObject 類別的新執行個體。Initializes a new instance of the ManagementObject class bound to the specified WMI path, including the specified additional options.

public:
 ManagementObject(System::Management::ManagementPath ^ path, System::Management::ObjectGetOptions ^ options);
public ManagementObject (System.Management.ManagementPath path, System.Management.ObjectGetOptions options);
new System.Management.ManagementObject : System.Management.ManagementPath * System.Management.ObjectGetOptions -> System.Management.ManagementObject
Public Sub New (path As ManagementPath, options As ObjectGetOptions)

參數

path
ManagementPath

ManagementPath,含有 WMI 路徑。A ManagementPath containing the WMI path.

options
ObjectGetOptions

ObjectGetOptions,含有繫結到 WMI 物件的其他選項。An ObjectGetOptions containing additional options for binding to the WMI object. 如果要採用預設的選項,這個參數可以是 Null。This parameter could be null if default options are to be used.

範例

下列範例會初始化類別的新實例,這個實例系結 ManagementObject 至特定的 WMI 路徑。The following example initializes a new instance of the ManagementObject class that is bound to a specific WMI path.

using System;
using System.Management;

class Sample
{
    public static int Main(string[] args)
    {
        ManagementPath p =
            new ManagementPath("Win32_Service");

        // Set options for no context info
        // but requests amended qualifiers
        // to be contained in the object
        ObjectGetOptions opt =
            new ObjectGetOptions(
            null, System.TimeSpan.MaxValue, true);

        ManagementClass c =
            new ManagementClass(p, opt);

        Console.WriteLine(
            c.Qualifiers["Description"].Value);

        return 0;
    }
}
Imports System.Management

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

        Dim p As New ManagementPath("Win32_Service")

        ' Set options for no context info
        ' but requests amended qualifiers 
        ' to be contained in the object
        Dim opt As New ObjectGetOptions( _
            Nothing, TimeSpan.MaxValue, True)

        Dim c As New ManagementClass(p, opt)

        Console.WriteLine(c.Qualifiers("Description").Value)

        Return 0
    End Function
End Class

備註

.NET Framework 安全性.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.

適用於

ManagementObject(SerializationInfo, StreamingContext)

初始化可序列化之 ManagementObject 類別的新執行個體。Initializes a new instance of the ManagementObject class that is serializable.

protected:
 ManagementObject(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
public:
 ManagementObject(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
protected ManagementObject (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
public ManagementObject (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
new System.Management.ManagementObject : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Management.ManagementObject
Protected Sub New (info As SerializationInfo, context As StreamingContext)
Public Sub New (info As SerializationInfo, context As StreamingContext)

參數

info
SerializationInfo

要以資料填入的 SerializationInfoThe SerializationInfo to populate with data.

context
StreamingContext

這個序列化的目的端 (請參閱 StreamingContext)。The destination (see StreamingContext) for this serialization.

備註

.NET Framework 安全性.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.

適用於

ManagementObject(String, ObjectGetOptions)

初始化繫結到指定的 WMI 路徑 (包括指定的其他選項) 之 ManagementObject 類別的新執行個體。Initializes a new instance of the ManagementObject class bound to the specified WMI path, including the specified additional options. 在這項變數中,路徑可以指定為字串。In this variant, the path can be specified as a string.

public:
 ManagementObject(System::String ^ path, System::Management::ObjectGetOptions ^ options);
public ManagementObject (string path, System.Management.ObjectGetOptions options);
new System.Management.ManagementObject : string * System.Management.ObjectGetOptions -> System.Management.ManagementObject
Public Sub New (path As String, options As ObjectGetOptions)

參數

path
String

通往物件的 WMI 路徑。The WMI path to the object.

options
ObjectGetOptions

ObjectGetOptions,表示取得指定 WMI 物件的選項。An ObjectGetOptions representing options to get the specified WMI object.

範例

下列範例會初始化類別的新實例 ManagementObjectThe following example initializes a new instance of the ManagementObject class.

using System;
using System.Management;

class Sample
{
    public static int Main(string[] args)
    {
        // Set options for no context info,
        // but requests amended qualifiers
        // to be contained in the object
        ObjectGetOptions opt =
            new ObjectGetOptions(null, System.TimeSpan.MaxValue, true);

        ManagementObject o =
            new ManagementObject(
            "Win32_Service", opt);

        Console.WriteLine(o.GetQualifierValue("Description"));

        return 0;
    }
}
Imports System.Management

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

        ' Set options for no context info, 
        ' but requests amended qualifiers
        ' to be contained in the object
        Dim opt As New ObjectGetOptions( _
            Nothing, System.TimeSpan.MaxValue, True)

        Dim o As New ManagementObject( _
            "Win32_Service", opt)

        Console.WriteLine(o.GetQualifierValue("Description"))

        Return 0
    End Function
End Class

備註

.NET Framework 安全性.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.

適用於

ManagementObject(ManagementScope, ManagementPath, ObjectGetOptions)

初始化繫結至指定的 WMI 路徑 (包括指定的選項) 之 ManagementObject 類別的新執行個體。Initializes a new instance of the ManagementObject class bound to the specified WMI path that includes the specified options.

public:
 ManagementObject(System::Management::ManagementScope ^ scope, System::Management::ManagementPath ^ path, System::Management::ObjectGetOptions ^ options);
public ManagementObject (System.Management.ManagementScope scope, System.Management.ManagementPath path, System.Management.ObjectGetOptions options);
new System.Management.ManagementObject : System.Management.ManagementScope * System.Management.ManagementPath * System.Management.ObjectGetOptions -> System.Management.ManagementObject
Public Sub New (scope As ManagementScope, path As ManagementPath, options As ObjectGetOptions)

參數

scope
ManagementScope

ManagementScope,表示 WMI 物件所在的範圍。A ManagementScope representing the scope in which the WMI object resides. 在這個版本中,範圍只能是 WMI 命名空間。In this version, scopes can only be WMI namespaces.

path
ManagementPath

ManagementPath,表示通往可管理物件的 WMI 路徑。A ManagementPath representing the WMI path to the manageable object.

options
ObjectGetOptions

ObjectGetOptions,指定取得物件的其他選項。An ObjectGetOptions specifying additional options for getting the object.

範例

下列範例會初始化類別的新實例,這個實例系結 ManagementObject 至特定的 WMI 路徑。The following example initializes a new instance of the ManagementObject class that is bound to a specific WMI path.

using System;
using System.Management;

class Sample
{
    public static int Main(string[] args)
    {
        ManagementScope s = new ManagementScope(
            "\\\\MyMachine\\root\\cimv2");
        ManagementPath p =
            new ManagementPath(
            "Win32_Service");

        // Set options for no context info,
        // but requests amended qualifiers
        // to be contained in the object
        ObjectGetOptions opt =
            new ObjectGetOptions(
            null, TimeSpan.MaxValue, true);

        ManagementObject o = new ManagementObject(s, p, opt);

        Console.WriteLine(o.Qualifiers["Description"].Value);

        return 0;
    }
}
Imports System.Management

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

        Dim s As New ManagementScope( _
            "\\MyMachine\root\cimv2")
        Dim p As New ManagementPath( _
            "Win32_Service")

        ' Set options for no context info,
        ' but requests amended qualifiers 
        ' to be contained in the object
        Dim opt As ObjectGetOptions
        opt = New ObjectGetOptions( _
                Nothing, TimeSpan.MaxValue, True)

        Dim o As ManagementObject
        o = New ManagementObject(s, p, opt)

        Console.WriteLine(o.Qualifiers("Description").Value)

        Return 0
    End Function
End Class

備註

由於 WMI 路徑可以是相對或完整,因此可能會發生範圍和指定路徑之間的衝突。Because WMI paths can be relative or full, a conflict between the scope and the path specified may arise. 但是,如果指定了範圍並指定了相對的 WMI 路徑,就不會發生衝突。However, if a scope is specified and a relative WMI path is specified, then there is no conflict. 以下是一些可能的衝突:The following are some possible conflicts:

如果未指定範圍,且指定了相對的 WMI 路徑,則範圍會預設為本機電腦 DefaultPathIf a scope is not specified and a relative WMI path is specified, then the scope will default to the local machine's DefaultPath.

如果未指定範圍,且指定了完整的 WMI 路徑,則會從完整路徑的範圍部分推斷範圍。If a scope is not specified and a full WMI path is specified, then the scope will be inferred from the scope portion of the full path. 例如,完整的 WMI 路徑: \\MyMachine\root\MyNamespace:MyClass.Name='abc' 將代表範圍 ' \MyMachine\root\MyNamespace ' 中的 wmi 物件 ' MyClass. Name = ' abc '」 \ 。For example, the full WMI path: \\MyMachine\root\MyNamespace:MyClass.Name='abc' will represent the WMI object 'MyClass.Name='abc'" in the scope '\\MyMachine\root\MyNamespace'.

如果指定了範圍,且指定了完整的 WMI 路徑,則範圍將覆寫完整路徑的範圍部分。If a scope is specified and a full WMI path is specified, then the scope will override the scope portion of the full path. 例如,如果指定了下列範圍: \ \MyMachine\root\MyScope,並指定了下列完整路徑: \ \MyMachine\root\MyNamespace: MyClass. Name = ' abc ',然後尋找下列 object: \\MyMachine\root\MyScope:MyClass.Name= 'abc' (完整路徑的範圍部分會被忽略) 。For example, if the following scope was specified: \\MyMachine\root\MyScope, and the following full path was specified: \\MyMachine\root\MyNamespace:MyClass.Name='abc', then look for the following object: \\MyMachine\root\MyScope:MyClass.Name= 'abc' (the scope part of the full path is ignored).

.NET Framework 安全性.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.

適用於

ManagementObject(String, String, ObjectGetOptions)

初始化繫結至指定的 WMI 路徑 (且包括指定的選項) 之 ManagementObject 類別的新執行個體。Initializes a new instance of the ManagementObject class bound to the specified WMI path, and includes the specified options. 範圍和路徑會指定為字串。The scope and the path are specified as strings.

public:
 ManagementObject(System::String ^ scopeString, System::String ^ pathString, System::Management::ObjectGetOptions ^ options);
public ManagementObject (string scopeString, string pathString, System.Management.ObjectGetOptions options);
new System.Management.ManagementObject : string * string * System.Management.ObjectGetOptions -> System.Management.ManagementObject
Public Sub New (scopeString As String, pathString As String, options As ObjectGetOptions)

參數

scopeString
String

WMI 物件的範圍。The scope for the WMI object.

pathString
String

WMI 物件路徑。The WMI object path.

options
ObjectGetOptions

ObjectGetOptions,表示取得 WMI 物件的其他選項。An ObjectGetOptions representing additional options for getting the WMI object.

範例

下列範例會 ManagementObject 使用特定的 WMI 路徑和選項,初始化類別的新實例。The following example initializes a new instance of the ManagementObject class with a specific WMI path and options.

using System;
using System.Management;

class Sample
{
    public static int Main(string[] args)
    {
        ObjectGetOptions opt =
            new ObjectGetOptions(null, System.TimeSpan.MaxValue, true);
        ManagementObject o =
            new ManagementObject(
            "root\\MyNamespace", "MyClass", opt);

        return 0;
    }
}
Imports System.Management

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

        Dim opt As New ObjectGetOptions( _
            Nothing, System.TimeSpan.MaxValue, True)
        Dim o As New ManagementObject( _
            "root\MyNamespace", "MyClass", opt)

        Return 0
    End Function
End Class

備註

如需詳細資料,請參閱相等的多載。See the equivalent overload for details.

.NET Framework 安全性.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.

適用於