ManagementObjectSearcher Class (System.Management)

Switch View :
ScriptFree
.NET Framework Class Library
ManagementObjectSearcher Class

Retrieves a collection of management objects based on a specified query. This class is one of the more commonly used entry points to retrieving management information. For example, it can be used to enumerate all disk drives, network adapters, processes and many more management objects on a system, or to query for all network connections that are up, services that are paused, and so on. When instantiated, an instance of this class takes as input a WMI query represented in an ObjectQuery or its derivatives, and optionally a ManagementScope representing the WMI namespace to execute the query in. It can also take additional advanced options in an EnumerationOptions. When the Get() method on this object is invoked, the ManagementObjectSearcher executes the given query in the specified scope and returns a collection of management objects that match the query in a ManagementObjectCollection.

Inheritance Hierarchy

System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      System.Management.ManagementObjectSearcher

Namespace:  System.Management
Assembly:  System.Management (in System.Management.dll)
Syntax

Visual Basic
Public Class ManagementObjectSearcher _
	Inherits Component
C#
public class ManagementObjectSearcher : Component
Visual C++
public ref class ManagementObjectSearcher : public Component
F#
type ManagementObjectSearcher =  
    class
        inherit Component
    end

The ManagementObjectSearcher type exposes the following members.

Constructors

  Name Description
Public method 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 default constructor.
Public method ManagementObjectSearcher(ObjectQuery) Initializes a new instance of the ManagementObjectSearcher class used to invoke the specified query for management information.
Public method ManagementObjectSearcher(String) Initializes a new instance of the ManagementObjectSearcher class used to invoke the specified query for management information.
Public method ManagementObjectSearcher(ManagementScope, ObjectQuery) Initializes a new instance of the ManagementObjectSearcher class used to invoke the specified query in the specified scope.
Public method ManagementObjectSearcher(String, String) Initializes a new instance of the ManagementObjectSearcher class used to invoke the specified query in the specified scope.
Public method 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.
Public method 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.
Top
Properties

  Name Description
Protected property CanRaiseEvents Gets a value indicating whether the component can raise an event. (Inherited from Component.)
Public property Container Gets the IContainer that contains the Component. (Inherited from Component.)
Protected property DesignMode Gets a value that indicates whether the Component is currently in design mode. (Inherited from Component.)
Protected property Events Gets the list of event handlers that are attached to this Component. (Inherited from Component.)
Public property Options Gets or sets the options for how to search for objects.
Public property Query Gets or sets the query to be invoked in the searcher (that is, the criteria to be applied to the search for management objects).
Public property Scope Gets or sets the scope in which to look for objects (the scope represents a WMI namespace).
Public property Site Gets or sets the ISite of the Component. (Inherited from Component.)
Top
Methods

  Name Description
Public method CreateObjRef Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public method Dispose() Releases all resources used by the Component. (Inherited from Component.)
Protected method Dispose(Boolean) Releases the unmanaged resources used by the Component and optionally releases the managed resources. (Inherited from Component.)
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection. (Inherited from Component.)
Public method Get() Invokes the specified WMI query and returns the resulting collection.
Public method Get(ManagementOperationObserver) Invokes the WMI query asynchronously, and binds to a watcher to deliver the results.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetLifetimeService Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected method GetService Returns an object that represents a service provided by the Component or by its Container. (Inherited from Component.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method InitializeLifetimeService Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected method MemberwiseClone() Creates a shallow copy of the current Object. (Inherited from Object.)
Protected method MemberwiseClone(Boolean) Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Public method ToString Returns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from Component.)
Top
Events

  Name Description
Public event Disposed Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)
Top
Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also

Reference

Community Content

jabberwoky
Example c++ cli to get a list of files in a path

#include "stdafx.h"
#using <system.dll>
using namespace System;
#using <System.Management.dll>
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Globalization;
using namespace System::Management;
using namespace System::IO;


String^ Search = "SELECT * FROM CIM_DataFile WHERE (Drive='C:') AND Path='\\\\Archive\\\\' AND Extension='vbs'";
ManagementObjectSearcher^ query = gcnew ManagementObjectSearcher(Search);
ManagementObjectCollection^ coll = query->Get();
for each (ManagementObject^ mo in coll)
{
Console::WriteLine(mo->GetPropertyValue("Name")->ToString());
Console::WriteLine(mo->GetPropertyValue("LastAccessed")->ToString());
Console::WriteLine(mo->GetPropertyValue("LastModified")->ToString());
Console::WriteLine(mo->GetPropertyValue("Path")->ToString());
Console::WriteLine(mo->GetPropertyValue("FileSize")->ToString());
Console::WriteLine(mo->GetPropertyValue("Extension")->ToString());
//Console::WriteLine("" + mo->ToString());
}