ManagementObjectSearcher Constructors

Definition

Initializes a new instance of the ManagementObjectSearcher class.

Overloads

ManagementObjectSearcher()

Initializes a new instance of the ManagementObjectSearcher class. After some properties on this object are set, the object can be used to invoke a query for management information. This is the parameterless constructor.

ManagementObjectSearcher(ObjectQuery)

Initializes a new instance of the ManagementObjectSearcher class used to invoke the specified query for management information.

ManagementObjectSearcher(String)

Initializes a new instance of the ManagementObjectSearcher class used to invoke the specified query for management information.

ManagementObjectSearcher(ManagementScope, ObjectQuery)

Initializes a new instance of the ManagementObjectSearcher class used to invoke the specified query in the specified scope.

ManagementObjectSearcher(String, String)

Initializes a new instance of the ManagementObjectSearcher class used to invoke the specified query in the specified scope.

ManagementObjectSearcher(ManagementScope, ObjectQuery, EnumerationOptions)

Initializes a new instance of the ManagementObjectSearcher class to be used to invoke the specified query in the specified scope, with the specified options.

ManagementObjectSearcher(String, String, EnumerationOptions)

Initializes a new instance of the ManagementObjectSearcher class used to invoke the specified query, in the specified scope, and with the specified options.

ManagementObjectSearcher()

Source:
ManagementObjectSearcher.cs
Source:
ManagementObjectSearcher.cs
Source:
ManagementObjectSearcher.cs

Initializes a new instance of the ManagementObjectSearcher class. After some properties on this object are set, the object can be used to invoke a query for management information. This is the parameterless constructor.

C#
public ManagementObjectSearcher();

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 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (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 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

ManagementObjectSearcher(ObjectQuery)

Source:
ManagementObjectSearcher.cs
Source:
ManagementObjectSearcher.cs
Source:
ManagementObjectSearcher.cs

Initializes a new instance of the ManagementObjectSearcher class used to invoke the specified query for management information.

C#
public ManagementObjectSearcher(System.Management.ObjectQuery query);

Parameters

query
ObjectQuery

An ObjectQuery representing the query to be invoked by the searcher.

Examples

The following example initializes a new instance of the ManagementObjectSearcher class with a specific query.

C#
using System;
using System.Management;

public class Sample
{
    public static void Main(string[] args)
    {
        SelectQuery q =
            new SelectQuery("Win32_Service",
                "State='Running'");
        ManagementObjectSearcher s =
            new ManagementObjectSearcher(q);

        foreach (ManagementObject service in s.Get())
        {
            // show the instance
            Console.WriteLine(service.ToString());
        }
    }
}

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 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (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 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

ManagementObjectSearcher(String)

Source:
ManagementObjectSearcher.cs
Source:
ManagementObjectSearcher.cs
Source:
ManagementObjectSearcher.cs

Initializes a new instance of the ManagementObjectSearcher class used to invoke the specified query for management information.

C#
public ManagementObjectSearcher(string queryString);

Parameters

queryString
String

The WMI query to be invoked by the object.

Examples

The following example initializes a new instance of the ManagementObjectSearcher class with a specific query.

C#
using System;
using System.Management;

public class Sample
{
    public static void Main(string[] args)
    {
        ManagementObjectSearcher s =
            new ManagementObjectSearcher(
                "SELECT * FROM Win32_Service");

        foreach (ManagementObject service in s.Get())
        {
            // show the instance
            Console.WriteLine(service.ToString());
        }
    }
}

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 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (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 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

ManagementObjectSearcher(ManagementScope, ObjectQuery)

Source:
ManagementObjectSearcher.cs
Source:
ManagementObjectSearcher.cs
Source:
ManagementObjectSearcher.cs

Initializes a new instance of the ManagementObjectSearcher class used to invoke the specified query in the specified scope.

C#
public ManagementObjectSearcher(System.Management.ManagementScope scope, System.Management.ObjectQuery query);

Parameters

scope
ManagementScope

A ManagementScope representing the scope in which to invoke the query.

query
ObjectQuery

An ObjectQuery representing the query to be invoked.

Examples

The following example initializes a new instance of the ManagementObjectSearcher class with a specific query and scope.

C#
using System;
using System.Management;

public class Sample
{
    public static void Main(string[] args)
    {
        ManagementScope myScope =
            new ManagementScope("root\\CIMV2");
        SelectQuery q =
            new SelectQuery("Win32_LogicalDisk");
        ManagementObjectSearcher s =
            new ManagementObjectSearcher(myScope,q);

        foreach (ManagementObject disk in s.Get())
        {
            // show the disk instance
            Console.WriteLine(disk.ToString());
        }
    }
}

Remarks

If no scope is specified, the default scope (DefaultPath) is used.

.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 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (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 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

ManagementObjectSearcher(String, String)

Source:
ManagementObjectSearcher.cs
Source:
ManagementObjectSearcher.cs
Source:
ManagementObjectSearcher.cs

Initializes a new instance of the ManagementObjectSearcher class used to invoke the specified query in the specified scope.

C#
public ManagementObjectSearcher(string scope, string queryString);

Parameters

scope
String

The scope in which to query.

queryString
String

The query to be invoked.

Examples

The following example initializes a new instance of the ManagementObjectSearcher class with a specific query and scope.

C#
using System;
using System.Management;

public class Sample
{
    public static void Main(string[] args)
    {
        ManagementObjectSearcher s =
            new ManagementObjectSearcher(
            "root\\CIMV2",
            "SELECT * FROM Win32_Service" +
            " WHERE State='Running'");

        foreach (ManagementObject service in s.Get())
        {
            // show the instance
            Console.WriteLine(service.ToString());
        }
    }
}

Remarks

If no scope is specified, the default scope (DefaultPath) is used.

.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 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (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 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

ManagementObjectSearcher(ManagementScope, ObjectQuery, EnumerationOptions)

Source:
ManagementObjectSearcher.cs
Source:
ManagementObjectSearcher.cs
Source:
ManagementObjectSearcher.cs

Initializes a new instance of the ManagementObjectSearcher class to be used to invoke the specified query in the specified scope, with the specified options.

C#
public ManagementObjectSearcher(System.Management.ManagementScope scope, System.Management.ObjectQuery query, System.Management.EnumerationOptions options);

Parameters

scope
ManagementScope

A ManagementScope specifying the scope of the query.

query
ObjectQuery

An ObjectQuery specifying the query to be invoked.

options
EnumerationOptions

An EnumerationOptions specifying additional options to be used for the query.

Examples

The following example initializes a new instance of the ManagementObjectSearcher class with a specific query, scope, and enumeration options.

C#
using System;
using System.Management;

public class Sample
{
    public static void Main(string[] args)
    {
        ManagementScope scope =
            new ManagementScope("root\\CIMV2");
        SelectQuery q =
            new SelectQuery("SELECT * FROM Win32_LogicalDisk");
        EnumerationOptions o =
            new EnumerationOptions(
            null, System.TimeSpan.MaxValue,
            1, true, false, true,
            true, false, true, true);
        ManagementObjectSearcher s =
            new ManagementObjectSearcher(scope, q, o);

        foreach (ManagementObject disk in s.Get())
        {
            // show the disk instance
            Console.WriteLine(disk.ToString());
        }
    }
}

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 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (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 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

ManagementObjectSearcher(String, String, EnumerationOptions)

Source:
ManagementObjectSearcher.cs
Source:
ManagementObjectSearcher.cs
Source:
ManagementObjectSearcher.cs

Initializes a new instance of the ManagementObjectSearcher class used to invoke the specified query, in the specified scope, and with the specified options.

C#
public ManagementObjectSearcher(string scope, string queryString, System.Management.EnumerationOptions options);

Parameters

scope
String

The scope in which the query should be invoked.

queryString
String

The query to be invoked.

options
EnumerationOptions

An EnumerationOptions specifying additional options for the query.

Examples

The following example initializes a new instance of the ManagementObjectSearcher class with a specific query, scope, and enumeration options.

C#
using System;
using System.Management;

public class Sample
{
    public static void Main(string[] args)
    {
        ManagementObjectSearcher s =
            new ManagementObjectSearcher(
            "root\\CIMV2",
            "SELECT * FROM Win32_Service",
            new EnumerationOptions(
            null, System.TimeSpan.MaxValue,
            1, true, false, true,
            true, false, true, true));

        foreach (ManagementObject service in s.Get())
        {
            // show the service
            Console.WriteLine(service.ToString());
        }
    }
}

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 9 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (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 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)