C # Restart code for remote computers _c# tutorial

Source: Internet
Author: User
If you are asked to implement a remote boot to someone else's computer, the first thing you might think of is to run a client program on a remote computer and then do a server-side program on the local computer to restart the remote computer with direct communication from these two programs. This is certainly a method. But that's a bit of a hassle. If you now only tell your remote computer's administrator's login account, and not allow you to run a so-called client program on a remote computer, you can use the program to complete the reboot of the remote computer. I don't know if you feel a bit difficult. In fact, according to the above conditions to achieve the restart of the remote computer, C # can be more convenient to complete. Here is a detailed introduction to the implementation of the method.
A C # Some theoretical knowledge of restarting a remote computer:
The C # Implementation of starting a remote computer is the "Windows Management specification." is called "WMI" (Windows Management instrumentation). Windows Management Instrumentation (WMI) supports the architecture of managing systems over the Internet. By providing a consistent view of the management environment, WMI provides users with common access management information. This management consistency allows you to manage the entire system, not just the components. From Microsoft MSDN, you can get more information about the WMI Software Development Kit (SDK).
WMI (Windows Management specification) supports a limited security format that allows users to authenticate each user before connecting to WMI on the local computer or on a remote computer. This security is another layer on the security top of an existing operating system. WMI does not overwrite or destroy any existing security provided by the operating system. By default, all members of the Administrators group have full control over the WMI service on the computer that it manages. All other users have only read/write/execute permissions on their local computer. You can change permissions by adding users to the Administrators group on the managed computer, or by authorizing the user or group in WMI and setting the permission level. Access is based on the WMI namespace. In general, the default namespace for a script program is "root\cimv2".
There are many features in WMI that make us feel amazing. Restarting a remote computer is only a small feature. Using WMI in your programs allows you to write applications for many remote administration types. Because of the namespaces in the. Net FrameWork SDK that allow you to manipulate WMI directly, C # can use the classes defined in these namespaces to make the most of the convenience that WMI controls bring us.
Two Design and operation of the environment settings:
(1). Windows Professional
(2). . Net FrameWork SDK
(3). Administrator account number of remote computer
These are not only the local computer configuration, but also the remote computer configuration.
Three Implementation restarts a remote computer using the. Net FrameWork SDK to manipulate WMI namespaces and classes:
Add Reference system.management;
The namespaces used in the. Net FrameWork SDK to manipulate WMI are primarily "system.management". There are six major classes to use to restart a remote computer:
. The "ConnectionOptions" class primarily defines the administrator account for a remote computer;
. "Managementscope" is primarily a computer that connects a given computer name or IP address with a given administrator account number;
. The "ObjectQuery" class feature is defined for remote operations to be implemented on remote computers;
. The "ManagementObjectSearcher" class obtains those WMI operations from a computer that has completed a remote connection;
. The "Managementobjectcollection" class is stored to get WMI operations;
. The "ManagementObject" class invokes a remote computer for WMI operations.
The operation described in this article is a restart operation.
Four C # Important steps and implementations for restarting a remote computer:
(1). Connect to a remote computer:
You can connect to a remote computer by following these statements:
ConnectionOptions options = new ConnectionOptions ();
Options. Username = "Manager account user name";
Options. Password = "Manager account password";
Managementscope scope = new Managementscope ("\\\\" + "remote computer name or IP address" + "\\root\\cimv2", options);
Connecting remote computers with a given administrator username and password
Scope. Connect ();
(2). Can be WMI controlled on a remote computer:
System.Management.ObjectQuery OQ = new System.Management.ObjectQuery ("SELECT * from Win32_OperatingSystem");
ManagementObjectSearcher Query1 = new ManagementObjectSearcher (scope, OQ);
Get WMI Control
Managementobjectcollection QueryCollection1 = Query1. Get ();
(3). Calling WMI control to implement a reboot of a remote computer:
foreach (ManagementObject mo in QueryCollection1)
{
string [] ss= {"};
Restart the remote computer
Mo. InvokeMethod ("Reboot", SS);
}
Five C # Implements the restart of the remote computer's source code (Boot.cs) and execution interface:
After understanding the C # Implementation of these important steps of restarting a remote computer, you can calmly get the complete code to restart the remote computer, as follows:
Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using System.Management;
Namespace Restartboot
{
<summary>
Summary description of the Form1.
</summary>
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;
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;
Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();
//
TODO: Add any constructor code after the InitializeComponent call
//
}
<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
#region Windows Form Designer generated code
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
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 = "Admin 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);
This.textBox1.TabIndex = 3;
This.textBox1.Text = "";
//
TextBox2
//
This.textBox2.Location = new System.Drawing.Point (136, 80);
This.textBox2.Name = "TextBox2";
This.textBox2.Size = new System.Drawing.Size (152, 21);
This.textBox2.TabIndex = 4;
This.textBox2.Text = "";
//
TextBox3
//
This.textBox3.Location = new System.Drawing.Point (136, 128);
This.textBox3.Name = "TextBox3";
This.textBox3.Size = new System.Drawing.Size (152, 21);
This.textBox3.TabIndex = 5;
This.textBox3.Text = "";
//
Button1
//
This.button1.Location = new System.Drawing.Point (104, 168);
This.button1.Name = "Button1";
This.button1.Size = new System.Drawing.Size (120, 23);
This.button1.TabIndex = 6;
This.button1.Text = "reboot remote computer";
This.button1.Click + = new System.EventHandler (This.button1_click);
//
Form1
//
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. ClientSize = new System.Drawing.Size (320, 213);
This. Controls.AddRange (new system.windows.forms.control[] {this.label1});
This. Name = "Form1";
This. Text = "Restart remote computer";
This. ResumeLayout (FALSE);
}
#endregion
<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}
private void Button1_Click (object sender, System.EventArgs e)
{
Defines some options for connecting to a remote computer
ConnectionOptions options=new connectionoptions ();
Options. Username=textbox2.text;
Options. Password=textbox3.text;
Managementscope scope=new managementscope ("\\\\" +textbox1.text+ "\\root\\cimv2", options);
Try
{
Connecting remote computers with a given administrator username and password
Scope. Connect ();
ObjectQuery oq=new objectquery ("SELECT * from Win32_OperatingSystem");
ManagementObjectSearcher query1=new ManagementObjectSearcher (SCOPE,OQ);
Managementobjectcollection Querycollection1=query1. Get ();
foreach (ManagementObject mo in QueryCollection1)
{
String[] ss={""};
Mo. InvokeMethod ("Reboot", SS);
}
}
catch (Exception er)
{
MessageBox.Show ("Connection" + TextBox1.Text +) error, the error message is: "+er. message);
}
}
}
}
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.