Author: sparon E-MAIL: ZKY@163.COM man: ZHAOKEYONG@HOTMAIL.COM
First, let's look at a piece of code.:
Managementscope conn = new managementscope ("\\\\" + textbox1.text + "\\ Root \ cimv2", options ); Conn. Connect (); Objectquery OQ = new objectquery ("select * From win32_operatingsystem "); Managementobjectsearcher query1 = new managementobjectsearcher (Conn, OQ ); Managementobjectcollection querycollection1 = query1.get (); Foreach (managementobject Mo in querycollection1) { String [] Ss = {""}; //Restart If (operationstr. Text ="Restart") { Mo. invokemethod ("reboot", SS ); } } |
This is a course of Operation computerCode,How do I feel after reading this code?,Is it like operating a database?,In fact, all of this is justified by the theme we are going to talk about today ---WMI.
After two days of researchWMII have some knowledge aboutWMII do not know much about it. Therefore, this article will only give a brief explanation and only serve as a reference. I also hope to explore more in depth.
WMI is the core of the Windows 2 k/XP Management System; for other Win32 operating systems, WMI is a useful plug-in. WMI Based on cimom , cimom Common Information Model Object Manager ) is an object database that describes the operating system components, provides an operating system access unit for MMC and script Programs . public interface. With WMI , different APIs are not required when tool software and script programs access different parts of the operating system. On the contrary, different parts of the operating system can be inserted. WMI , 1. ( the figure is from msdn) , tool software and WMI allow you to easily read and write WMI . (From: use WMI to implement Windows Automatic System Management )
Figure 1
WMITool software:Windows Management Instrumentation (Wmi) SDK
PassWindows Management Instrumentation extensions for Microsoft Visual Studio. NET 2003 Server ExplorerTools availableVs2003EasyWMIProgramming.
Windows Management Instrumentation (Wmi) ToolsTools to conveniently view availableCIMAndWin32Class
Now, let's go back to our example and analyze it.
First, we createdManagementscopeOfInstance, Indicates initialization with the specified optionManagementscopeClass, indicating the new instance of the specified range path.
Conn. Connect ();Needless to say, it is to open the object connection.
Then,ObjectqueryObject, and passed in a string, the key is in this string, it looks likeTrance-SQLStatement, but it is actuallyWMI. As shown in, the location of our code is[Win32 subsystem]WMIComparedSQLDatabase, thisWin32_operatingsystemThe class is equivalent to the table in the database. This is easy to understand, but the actual theory should be clear.
Now, we need to instantiate the query object --Managementobjectsearcher. Database operations. In the same way, transfer a connection object and a query object.OK.
Managementobjectsearcher. Get ()The method is to call the specifiedWMIQuery and return result set.
Then, recursively repeat and read the returned results and perform operations.InvokemethodMethod.
The method is prototype:
Public object invokemethod ( String methodname ,//Name of the method to be operated Object [] ARGs //Array containing parameter values )//This method returns the object Value |
Well, it's almost all done. What I want to say at the end isWin32_operatingsystemClass.
Win32_operatingsystemClass is nativeAPIFunction (use a scriptWMIProgramming cannot be accessed directlyWin32 API)
The specific methods are as follows (from:Msdn libraries for Visual Studio 2005)
Reboot |
Class method that shuts down the computer system, and then restarts it. |
Setdatetime |
Class method that allows setting the computer date and time. |
Shutdown |
Class method that unloads programs and DLLs to the point where it is safe to turn off the computer. |
Win32shutdown |
Class method that provides the full set of shutdown options supported by Windows operating systems. |
Let's take a lookMsdnExample above
Using system; Using system. Management; Public class invokemethod { Public static void main () { Managementclass processclass = new managementclass ("win32_process "); Object [] methodargs = {"notepad.exe", null, null, 0 }; Object result = processclass. invokemethod ("CREATE", methodargs ); Console. writeline ("creation of process returned:" + result ); Console. writeline ("process ID:" + methodargs [3]); } } |
Finally, let me give a brief summary of our example:WMIThis allows us to operate a route computer as easily as a database. The steps required are as follows:
1.Create a connection object;
2.Open the connection object;
3.Create a connection channel object;
4.Construct a query using the connection object and connection channel object;
5.Obtain query results;
6.Specific operation.
Works: Remote Computer Control Using WMI
Appendix: References
《Msdn libraries for Visual Studio 2005"
Msdn libray: WMIScript entry
UseWMIImplementationWindowsAutomatic System Management
Wmi cim Studio