If you want to remotely start someone else's computer, the first thing you think of is to first run a client program on the remote computer and then create another program on the local computer.
ServerThe remote computer is restarted through direct communication between the two programs. This is of course a method. However, this is a little troublesome. If you only tell the administrator of the remote computer to log on to the account, you are not allowed to run a so-called client program on the remote computer, so that you can restart the remote computer through the program. I wonder if you feel a little difficult. In fact, restarting the remote computer based on the above conditions can be easily completed using C. The following describes the specific implementation methods.
I. C # Some theoretical knowledge about restarting remote computers:
C # The principle for enabling remote computers is "Windows Management specifications ". WMI (Windows Management Instrumentation ). Windows Management specifications (Wmi) support the structure of management systems over the Internet. By providing consistent observation of the management environment, WMI provides users with general access management information. The consistency of management enables you to manage the entire system, not just components. From Microsoft msdn, you can obtain detailed information about the WMI software development kit (SDK.
WMI (Windows Management specifications) supports limited security formats, allowing users to verify each user before connecting to WMI on a local computer or remote computer. This security isOperating SystemAnother layer of the existing security top. WMI does not cover or damageOperating SystemProvides any existing security. By default, all members in the Administrator Group have full control over the WMI services on the computers it manages. All other users only have read, write, and execute permissions on their local computers. You can change permissions by adding users to the Administrator Group on the managed computer, or authorizing users or groups in WMI and setting the permission level. Access is based on the WMI namespace. In general, the default namespace of the script program is "root/cimv2 ".
There are many features in WMI that can surprise us. Restarting a remote computer is only a small function. WMI can be used in programs to compile many remote management types.ApplicationProgram. Because. the Net Framework SDK provides namespaces that allow you to directly operate WMI, so C # can use the classes defined in these namespaces to make full use of WMI control to bring us various conveniences.
2. environment settings for program design and operation:
(1). Windows 2000ServerVersion
(2). Net Framework SDK beta 2
(3) Administrator Account of Remote Computer
These are not only local computer configurations, but also remote computer configurations.
3. Restart the remote computer to Use WMI namespaces and classes in. NET Framework SDK beta 2:
In. NET Framework SDK beta 2, The namespace used to operate WMI is mainly "system. Management ". There are six main categories to restart a remote computer:
. "Connectionoptions" class mainly defines the Administrator account of the remote computer;
. "Managementscope" is used to connect a computer with a given computer name or IP address with a given administrator account;
The "objectquery" class function defines the remote operations to be performed on remote computers;
The. "managementobjectsearcher" class obtains the WMI operations from the computers that have completed remote connection;
. "Managementobjectcollection" class is stored for WMI operations;
. "Managementobject" class calls a remote computer to perform WMI operations.
The operation described in this article is the restart operation.
IV. C # important steps and implementation methods for restarting a remote computer:
(1). connect to a remote computer:
Use the following statements to connect to a remote computer:
Connectionoptions Options = new connectionoptions ();
Options. Username = "Manager account username ";
Options. PassWord= "Manager account password ";
Managementscope scope = new managementscope ("//" + "remote computer name or IP address" + "// root // cimv2", options );
// Connect the remote computer with the given administrator username and password
Scope. Connect ();
(2). WMI control can be performed on remote computers:
System. Management. objectquery OQ = new system. Management. objectquery ("select * From win32_operatingsystem ");
Managementobjectsearcher query1 = new managementobjectsearcher (scope, OQ );
// Obtain WMI Control
Managementobjectcollection querycollection1 = query1.get ();
(3). Call WMI control to restart the remote computer:
Foreach (managementobject Mo in querycollection1)
{
String [] Ss = {""};
// Restart the remote computer
Mo. invokemethod ("reboot", SS );
}