Use C # to restart a remote computer

Source: Internet
Author: User
If you want to remotely start someone else's computer, the first thing you think of is to first run the client program on the remote computer and then create another server program on the local computer, restart the remote computer 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 layer is another layer at the top of the existing security of the operating system. WMI does not cover or damage any existing security provided by the operating system. 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 write many remote management applications. 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 2000 Professional

(2). Net FrameWork SDK

(3) Administrator Account of Remote Computer

These are not only local computer configurations, but also remote computer configurations.

3. Restart the remote computer. Use the. Net FrameWork SDK to operate WMI namespaces and classes:

Add reference System. Management;

The namespace used to operate WMI in. Net FrameWork SDK 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 );

}

V. C # restart the source code (boot. cs) and execution interface of the remote computer:

After learning about the important steps of C # to restart a remote computer, you can easily get the complete code for restarting the remote computer, as shown below:

Using System;

Using System. Drawing;

Using System. Collections;

Using System. ComponentModel;

Using System. Windows. Forms;

Using System. Data;

Using System. Management;

Namespace ReStartboot

{

///

/// Summary of Form1.

///

Public class Form1: System. Windows. Forms. Form

{

Private System. Drawing. Printing. PrintDocument printDocument1;

Private System. Windows. Forms. Label label1;

Private System. Windows. Forms. Label label2;

Private System. Windows. Forms. Label label3;

Private System. Windows. Forms. TextBox textBox1;

Private System. Windows. Forms. TextBox textBox2;

Private System. Windows. Forms. TextBox textBox3;

Private System. Windows. Forms. Button button1;

///

/// Required designer variables.

///

Private System. ComponentModel. Container components = null;

Public Form1 ()

{

//

// Required for Windows Form Designer support

//

InitializeComponent ();

//

// TODO: add Any constructor code after InitializeComponent calls

//

}

///

/// Clear all resources in use.

///

Protected override void Dispose (bool disposing)

{

If (disposing)

{

If (components! = Null)

{

Components. Dispose ();

}

}

Base. Dispose (disposing );

}

# Region Windows Form Designer generated code

///

/// The designer supports the required methods-do not use the code editor to modify

/// Content of this method.

///

Private void InitializeComponent ()

{

This. printDocument1 = new System. Drawing. Printing. PrintDocument ();

This. label1 = new System. Windows. Forms. Label ();

This. label2 = new System. Windows. Forms. Label ();

This. label3 = new System. Windows. Forms. Label ();

This. textBox1 = new System. Windows. Forms. TextBox ();

This. textBox2 = new System. Windows. Forms. TextBox ();

This. textBox3 = new System. Windows. Forms. TextBox ();

This. button1 = new System. Windows. Forms. Button ();

This. SuspendLayout ();

//

// Label1

//

This. label1.Location = new System. Drawing. Point (16, 32 );

This. label1.Name = "label1 ";

This. label1.Size = new System. Drawing. Size (120, 23 );

This. label1.TabIndex = 0;

This. label1.Text = "remote computer name or IP :";

This. label1.TextAlign = System. Drawing. ContentAlignment. MiddleRight;

//

// Label2

//

This. label2.Anchor = System. Windows. Forms. AnchorStyles. Top;

This. label2.Location = new System. Drawing. Point (32, 80 );

This. label2.Name = "label2 ";

This. label2.TabIndex = 1;

This. label2.Text = "Administrator name :";

This. label2.TextAlign = System. Drawing. ContentAlignment. MiddleRight;

//

// Label3

//

This. label3.Location = new System. Drawing. Point (32,128 );

This. label3.Name = "label3 ";

This. label3.TabIndex = 2;

This. label3.Text = "Password :";

This. label3.TextAlign = System. Drawing. ContentAlignment. MiddleRight;

//

// TextBox1

//

This. textBox1.Location = new System. Drawing. Point (136, 32 );

This. textBox1.Name = "textBox1 ";

This. textBox1.Size = new System. Drawing. Size (152, 21 );

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.