Читати англійською Редагувати

Поділитися через


ManagementObject Constructors

Definition

Initializes a new instance of the ManagementObject class.

Overloads

ManagementObject()

Initializes a new instance of the ManagementObject class. This is the parameterless constructor.

ManagementObject(ManagementPath)

Initializes a new instance of the ManagementObject class for the specified WMI object path. The path is provided as a ManagementPath.

ManagementObject(String)

Initializes a new instance of the ManagementObject class for the specified WMI object path. The path is provided as a string.

ManagementObject(ManagementPath, ObjectGetOptions)

Initializes a new instance of the ManagementObject class bound to the specified WMI path, including the specified additional options.

ManagementObject(SerializationInfo, StreamingContext)
Obsolete.

Initializes a new instance of the ManagementObject class that is serializable.

ManagementObject(String, ObjectGetOptions)

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)

Initializes a new instance of the ManagementObject class bound to the specified WMI path that includes the specified options.

ManagementObject(String, String, ObjectGetOptions)

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()

Source:
ManagementObject.cs
Source:
ManagementObject.cs
Source:
ManagementObject.cs

Initializes a new instance of the ManagementObject class. This is the parameterless constructor.

C#
public ManagementObject();

Examples

The following example initializes a new instance of the ManagementObject class with the parameterless constructor.

C#
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;
    }
}

Remarks

.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

.NET 10 (package-provided) та інші версії
Продукт Версії
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

ManagementObject(ManagementPath)

Source:
ManagementObject.cs
Source:
ManagementObject.cs
Source:
ManagementObject.cs

Initializes a new instance of the ManagementObject class for the specified WMI object path. The path is provided as a ManagementPath.

C#
public ManagementObject(System.Management.ManagementPath path);

Parameters

path
ManagementPath

A ManagementPath that contains a path to a WMI object.

Examples

The following example initializes a new instance of the ManagementObject class with a specified WMI object path.

C#
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;
    }
}

Remarks

.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

.NET 10 (package-provided) та інші версії
Продукт Версії
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

ManagementObject(String)

Source:
ManagementObject.cs
Source:
ManagementObject.cs
Source:
ManagementObject.cs

Initializes a new instance of the ManagementObject class for the specified WMI object path. The path is provided as a string.

C#
public ManagementObject(string path);

Parameters

path
String

A WMI path.

Examples

The following example initializes a new instance of the ManagementObject class.

C#
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;
    }
}

Remarks

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

.NET 10 (package-provided) та інші версії
Продукт Версії
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

ManagementObject(ManagementPath, ObjectGetOptions)

Source:
ManagementObject.cs
Source:
ManagementObject.cs
Source:
ManagementObject.cs

Initializes a new instance of the ManagementObject class bound to the specified WMI path, including the specified additional options.

C#
public ManagementObject(System.Management.ManagementPath path, System.Management.ObjectGetOptions options);

Parameters

path
ManagementPath

A ManagementPath containing the WMI path.

options
ObjectGetOptions

An ObjectGetOptions containing additional options for binding to the WMI object. This parameter could be null if default options are to be used.

Examples

The following example initializes a new instance of the ManagementObject class that is bound to a specific WMI path.

C#
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;
    }
}

Remarks

.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

.NET 10 (package-provided) та інші версії
Продукт Версії
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

ManagementObject(SerializationInfo, StreamingContext)

Source:
ManagementObject.cs
Source:
ManagementObject.cs
Source:
ManagementObject.cs

Caution

This API supports obsolete formatter-based serialization. It should not be called or extended by application code.

Initializes a new instance of the ManagementObject class that is serializable.

C#
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected ManagementObject(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
C#
public ManagementObject(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
C#
protected ManagementObject(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);

Parameters

info
SerializationInfo

The SerializationInfo to populate with data.

context
StreamingContext

The destination (see StreamingContext) for this serialization.

Attributes

Remarks

.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

.NET 10 (package-provided) та інші версії
Продукт Версії (Застаріло.)
.NET (8 (package-provided), 9 (package-provided), 10 (package-provided))
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

ManagementObject(String, ObjectGetOptions)

Source:
ManagementObject.cs
Source:
ManagementObject.cs
Source:
ManagementObject.cs

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.

C#
public ManagementObject(string path, System.Management.ObjectGetOptions options);

Parameters

path
String

The WMI path to the object.

options
ObjectGetOptions

An ObjectGetOptions representing options to get the specified WMI object.

Examples

The following example initializes a new instance of the ManagementObject class.

C#
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;
    }
}

Remarks

.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

.NET 10 (package-provided) та інші версії
Продукт Версії
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

ManagementObject(ManagementScope, ManagementPath, ObjectGetOptions)

Source:
ManagementObject.cs
Source:
ManagementObject.cs
Source:
ManagementObject.cs

Initializes a new instance of the ManagementObject class bound to the specified WMI path that includes the specified options.

C#
public ManagementObject(System.Management.ManagementScope scope, System.Management.ManagementPath path, System.Management.ObjectGetOptions options);

Parameters

scope
ManagementScope

A ManagementScope representing the scope in which the WMI object resides. In this version, scopes can only be WMI namespaces.

path
ManagementPath

A ManagementPath representing the WMI path to the manageable object.

options
ObjectGetOptions

An ObjectGetOptions specifying additional options for getting the object.

Examples

The following example initializes a new instance of the ManagementObject class that is bound to a specific WMI path.

C#
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;
    }
}

Remarks

Because WMI paths can be relative or full, a conflict between the scope and the path specified may arise. However, if a scope is specified and a relative WMI path is specified, then there is no conflict. The following are some possible conflicts:

If a scope is not specified and a relative WMI path is specified, then the scope will default to the local machine's DefaultPath.

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

If a scope is specified and a full WMI path is specified, then the scope will override the scope portion of the full path. 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 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

.NET 10 (package-provided) та інші версії
Продукт Версії
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

ManagementObject(String, String, ObjectGetOptions)

Source:
ManagementObject.cs
Source:
ManagementObject.cs
Source:
ManagementObject.cs

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.

C#
public ManagementObject(string scopeString, string pathString, System.Management.ObjectGetOptions options);

Parameters

scopeString
String

The scope for the WMI object.

pathString
String

The WMI object path.

options
ObjectGetOptions

An ObjectGetOptions representing additional options for getting the WMI object.

Examples

The following example initializes a new instance of the ManagementObject class with a specific WMI path and options.

C#
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;
    }
}

Remarks

See the equivalent overload for details.

.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

.NET 10 (package-provided) та інші версії
Продукт Версії
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)