WMI can not only obtain the desired computer data, but also be used for remote control. Remote Computer Control is not only the dream of hackers, but also the desire of most network managers, especially in modern networks, it is made up of a large computer group. It is especially important to effectively manage every computer in the network. Currently, network management software generally runs client background programs on a remote computer and a server foreground control program on a local computer, remote control of the computer is achieved through direct communication between the two programs. The disadvantage of this method is very obvious. When the client closes the background program, this remote management cannot be implemented, so in order to overcome this shortcoming, the client program of the remote control software is very concealed, so that the user can run the program unconsciously.
In fact, WMI is a good choice for remote control software. Especially at present, Windows 2000 has become the mainstream operating system, using WMI to write remote control software can omit the distribution client program, which is the biggest headache for remote control software.
Copyright Disclaimer: Any website authorized by Skynet must retain the author's information and the following links during Reprinting
Tianji development C # column: http://dev.yesky.com/msdn/msdnc/
The remote control program described in this section allows users to restart and shut down remote computers. WMI is used. The following describes how to use WMI to control remote computers in Visual C.
1. start Visual Studio. and select "file", "new", and "project". In the displayed "new project" dialog box, set "project type" to "Visual C # Project ", set "template" to "Windows application", enter "Control Remote Computer Using WMI" in the "name" text box, and enter "E: /.. Net project, and then click OK. In this case. A new folder named "Control Remote Computer with WMI" is created in the "Net project" Directory, which stores the project file "Control Remote Computer with WMI.
2. Execute steps 2 to 4 in the "obtain remote computer hard disk information" project.
3. set Visual Studio. net current window switch to the form1.cs (Design) window, and drag the following components into the form from the Windows Forms components tab in the toolbox, and perform the corresponding operations:
Three textbox components, used to enter the remote computer name (or IP address), remote computer WMI operation user name and password.
A ComboBox component used to select the operation type for remote computers.
Four label components.
A button component named button1. After dragging the form, double-click the component, the system will automatically generate the processing code corresponding to its click event in the form1.cs file.
4. Adjust the values corresponding to each component attribute according to the data in Table 06:
Component Type |
Component name |
Attribute |
Setting result |
Form |
Form1 |
Text |
Use WMI to control remote computers |
Form1 |
Formborderstyle |
Fixedsingle |
Form1 |
Maximizebox |
False |
Label |
Label1 |
Text |
Machine name or IP Address: |
Label2 |
Text |
Super User Name with WMI permissions: |
Label3 |
Text |
Password: |
Label4 |
Text |
Select Control Type: |
Textbox |
Textbox1 |
Text |
"" |
Textbox2 |
Text |
"" |
Textbox3 |
Passwordchar |
* |
Textbox3 |
Text |
"" |
Button |
Button1 |
Text |
Run |
Button1 |
Flatstyle |
Flat |
Table 06: Using WMI to control remote computers
Adjust the positions and order of the components in the form according to the positions and order of the components in Figure 09:
Figure 09: Design Interface for Remote Computer Control Using WMI |
5. Switch the current window of Visual Studio. NET to the editing window of the form1.cs file, and replace the processing code corresponding to the click event of button1 in form1.cs with the following code. The following code is used to remotely control the remote computer based on the user's choice:
Private void button#click (Object sender, system. eventargs E) { // Determine the range of WMI operations Connectionoptions Options = new connectionoptions (); // Set the user name used for WMI connection operations Options. Username = textbox2.text; // Set the user's password Options. Password = textbox3.text; Try { Managementscope conn = new managementscope ("//" + textbox1.text + "// root // cimv2", options ); Conn. Connect (); // Determine the content of the WMI operation Objectquery OQ = new objectquery ("select * From win32_operatingsystem "); Managementobjectsearcher query1 = new managementobjectsearcher (Conn, OQ ); // Obtain the WMI operation content Managementobjectcollection querycollection1 = query1.get (); // Perform the corresponding remote operation based on the user's selection Foreach (managementobject Mo in querycollection1) { String [] Ss = {""}; // Restart the instance If (combobox1.text = "restart ") { Mo. invokemethod ("reboot", SS ); } Else // Execute remote shutdown If (combobox1.text = "remote shutdown ") { Mo. invokemethod ("shutdown", SS ); } Else MessageBox. Show ("incorrect operation selected! "," Error! "); } } // Error Catch (exception ee) { MessageBox. Show ("connection" + textbox1.text + "error, error message:" + ee. Message ); } } |
6. Now, after the above steps are completed correctly and all are saved, all the work in [Control Remote Computer with WMI] is completed. Click the [F5] shortcut to run the program. Enter the name or IP address of the remote computer to be controlled in the computer name or IP address text box of the program, in the user name with WMI permissions and Password text box, enter the account and password with WMI operations, and select the control type for the remote computer in the Select Control Type dialog box, click the execute button to control the specified remote computer. Figure 10 shows the running interface of "Using WMI to control remote computers.
Figure 10: Running Interface Using WMI to control remote computers |
Summary:
Through the above example, we can see that WMI is indeed a very useful thing, but because it is still relatively new, there is no complete introduction material in China or even abroad, therefore, I simply browsed the WMI application and hoped that the content in this article could help you understand and understand how to use Visual C # To Call WMI.