"WMI-6" WMI for C # dotnet support for WMI

Source: Internet
Author: User
Tags dotnet win32
dotnet Support for WMI

. NET Framework, there are two namespaces that are related to the WMI specification, namely the System.Management and System.Management.Instrumentation namespaces. The former is used to access the functionality provided by a standard WMI provider, to obtain system information, or to execute a provider's methods (such as modifying a system's properties, performing a shutdown action, etc.), and the latter is used to register a new provider, create a new class, and publish an instance.

The classes in the System.Management namespace are mainly:
managementclass--Management class
The ManagementClass constructor input parameter is the WMI class name, which identifies the management object (equivalent to the iwbemservices in COM), for example:
Diskman = new ManagementClass ("Win32_LogicalDisk"); Diskman represents the management object for the entire logical disk under the Win32 platform.

managementobjectcollection--A collection of managed objects. (equivalent to the enumerator in COM ienumwbemclassobject)

managementobject--is a specific data management object (equivalent to the iwbemclassobject in COM).
For the above example, the C partition management object for the logical disk can be mapped. Look at the following code:
managementobjectcollection disks = Diskman. GetInstances ();
foreach (ManagementObject disk in disks)
... {
Console.WriteLine ("Disk = {0}", disk["Name"). ToString ());
Console.WriteLine ("Disk = {0}", disk["filesystem"]. ToString ());
Console.WriteLine ("Disk = {0}", disk["Size"]. ToString ());
Disks is a logical disk partition c,d,e ... , for each logical partition, a disk object manages its data and outputs the name, file system, and disk space size.

Managementquery
As the basis for all query classes, as an abstract base class for queries, you can inherit to implement its methods, which are declared abstract, which means that we cannot create an instance of the class, but we can use objects of that type to represent all of its derived class objects. NET provides two derived classes from Managementquery: EventQuery and ObjectQuery are used for query management events and Management objects, and the selectquery we usually use is derived from ObjectQuery, see for details. NET Class Library Reference.

ManagementObjectSearcher
Used to retrieve a collection of management objects based on a specified query or enumeration.
SelectQuery selectquery = new SelectQuery ("Win32_LogicalDisk");
ManagementObjectSearcher searcher =
New ManagementObjectSearcher (SelectQuery);
foreach (ManagementObject disk in searcher. Get ())
... {
Console.WriteLine (disk. ToString ());
The above code specifies that the searcher query is SelectQuery, that is, the Win32 Logical disk's collection query, and then the logical partitions are exported. You can also instantiate a ManagementObjectSearcher object directly by using the WQL query statement:
ManagementObjectSearcher search =
New ManagementObjectSearcher ("SELECT * from Win32_ logicaldisk");
ManagementEventWatcher
Used to subscribe to WMI event notifications.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.