"WMI-5" WMI for C#/dotnet get system information with WMI

Source: Internet
Author: User
Tags dotnet
Like c++/com, you can query and Invoke methods in C # using dotnet.

The basic process of the query is as follows:
1. A. NET class library referencing WMI
Add a reference to the Dll:System.Management.dll of the System.Management namespace in the project-> reference.
2. Create an instance of the ManagementObjectSearcher class
The ManagementObjectSearcher class is used to retrieve a collection of management objects based on a specified query or enumeration.
3. Execute Query
The ManagementObjectSearcher class's Get method is used to execute the query
A sync mode:
The default query method is to query the thread to wait for the method to return after the query is executed, and the Query method returns after all result sets have been obtained. This way is suitable and the query data quantity is small, the inquiry time is very short situation.
In the case of synchronization, returns a managementobjectcollection that contains the object that matches the specified query.
Managementobjectcollection result = search. Get ();
foreach (ManagementObject info in result)
... {
Console.WriteLine ("Name: {0}", info["name"])
B Asynchronous Mode:
Asynchronous queries need to pass in a parameter of type Managementoperationobserver when executing the query method. After querying to the object, it is notified by the Objectready event of the Managementoperationobserver object that the object in the query is in the NewObject attribute of the Objectready event parameter.
In asynchronous cases, each query to an object, triggering a Managementoperationobserver class of the Objectready event, we get the value of the object in the event process.
Managementoperationobserver results = new
Managementoperationobserver ();

Connect an event handler
Results. Objectready + = new
Objectreadyeventhandler (this. NewObject);
results.completed + = new
Completedeventhandler (this. Done);

Searcher. Get (results);
Event Handling Process:
private void NewObject (object sender,
Objectreadyeventargs obj)
... {
Console.WriteLine ("Name: {0}", info["name"])
}
private void done (object sender,
CompletedEventArgs obj)
... {
IsCompleted = true;
4. Processing results
In the synchronous query mode, we can get the matching object by enumerating the elements inside the Managementobjectcollection object; In asynchronous mode, the matching object is fetched through the NewObject property of the Objectreadyeventargs object.

The basic procedure for invoking a method is as follows:
1. Get the provider class or its instance object
ManagementClass Processclass = new ManagementClass ("Win32_Process"); This line of code results in a Win32_Process class, but no instances of it have been generated. Through it, only win32_process static methods can be invoked.
ManagementObject classinstance =
New ManagementObject ("Root/cimv2",
"Win32_service.name= ' Plugplay '",
NULL); This line of code obtains an instance of the Win32_Service class, through which you can invoke Win32_Service static methods and instance methods.
2. To generate an object that provider the parameters of the method supplied
Provides an array of objects, or an object of type Managementbaseobject, that stores the arguments of the calling function, and then passes the object array or Managementbaseobject to the function that executes the provider method.
3. Calling methods
Call the ManagementClass or ManagementObject InvokeMethod () method to execute the provider method.

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.