这个例子参考参考。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;
using System.IO.IsolatedStorage;
using System.ComponentModel.Design.Serialization;
public class Form1 : Form
{
private TextBox textBox1 ;
private TextBox textBox2 ;
private TextBox textBox3 ;
private Label label1 ;
private Label label2 ;
private Label label3 ;
private Button button1 ;
private System.ComponentModel.Container components = null ;
public Form1 ( )
{
//初始化窗体中的各个组件
InitializeComponent ( ) ;
}
//清除程序中使用过的资源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}
private void InitializeComponent ( )
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(140, 46);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(172, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(138, 85);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(174, 21);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(139, 120);
this.textBox3.Name = "textBox3";
this.textBox3.PasswordChar = '*';
this.textBox3.Size = new System.Drawing.Size(173, 21);
this.textBox3.TabIndex = 2;
this.textBox3.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 50);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(120, 16);
this.label1.TabIndex = 1;
this.label1.Text = "机器名称或IP地址:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(37, 88);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "管理者名称:";
this.label2.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// label3
//
this.label3.Location = new System.Drawing.Point(37, 125);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(100, 16);
this.label3.TabIndex = 1;
this.label3.Text = "管理者密码:";
this.label3.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// button1
//
this.button1.Location = new System.Drawing.Point(95, 168);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(136, 32);
this.button1.TabIndex = 3;
this.button1.Text = "重新启动远程计算机";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(336, 245);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.textBox1,
this.textBox2,
this.textBox3,
this.label1,
this.label2,
this.label3});
this.Name = "Form1";
this.Text = "利用C#重新启动远程计算机";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
private void button1_Click ( object sender , System.EventArgs e )
{
//定义连接远程计算机的一些选项
ConnectionOptions options = new ConnectionOptions ( ) ;
options.Username = textBox2.Text ;
options.Password = textBox3.Text ;
ManagementScope scope = new ManagementScope( "\\\\" + textBox1.Text + "\\root\\cimv2", options ) ;
try
{
//用给定管理者用户名和口令连接远程的计算机
scope.Connect ( ) ;
System.Management.ObjectQuery oq = new System.Management.ObjectQuery ( "SELECT * FROM Win32_OperatingSystem" ) ;
ManagementObjectSearcher query1 = new ManagementObjectSearcher ( scope , oq ) ;
//得到WMI控制
ManagementObjectCollection queryCollection1 = query1.Get ( ) ;
foreach ( ManagementObject mo in queryCollection1 )
{
string [ ] ss= { "" } ;
//重启远程计算机
mo.InvokeMethod ( "Reboot" , ss ) ;
}
}
//报错
catch ( Exception ee )
{
MessageBox.Show ( "连接" + textBox1.Text + "出错,出错信息为:" + ee.Message ) ;
}
} private void Form1_Load(object sender, System.EventArgs e)
{
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;
using System.IO.IsolatedStorage;
using System.ComponentModel.Design.Serialization;
public class Form1 : Form
{
private TextBox textBox1 ;
private TextBox textBox2 ;
private TextBox textBox3 ;
private Label label1 ;
private Label label2 ;
private Label label3 ;
private Button button1 ;
private System.ComponentModel.Container components = null ;
public Form1 ( )
{
//初始化窗体中的各个组件
InitializeComponent ( ) ;
}
//清除程序中使用过的资源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}
private void InitializeComponent ( )
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(140, 46);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(172, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(138, 85);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(174, 21);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(139, 120);
this.textBox3.Name = "textBox3";
this.textBox3.PasswordChar = '*';
this.textBox3.Size = new System.Drawing.Size(173, 21);
this.textBox3.TabIndex = 2;
this.textBox3.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 50);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(120, 16);
this.label1.TabIndex = 1;
this.label1.Text = "机器名称或IP地址:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(37, 88);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "管理者名称:";
this.label2.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// label3
//
this.label3.Location = new System.Drawing.Point(37, 125);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(100, 16);
this.label3.TabIndex = 1;
this.label3.Text = "管理者密码:";
this.label3.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// button1
//
this.button1.Location = new System.Drawing.Point(95, 168);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(136, 32);
this.button1.TabIndex = 3;
this.button1.Text = "重新启动远程计算机";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(336, 245);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.textBox1,
this.textBox2,
this.textBox3,
this.label1,
this.label2,
this.label3});
this.Name = "Form1";
this.Text = "利用C#重新启动远程计算机";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
private void button1_Click ( object sender , System.EventArgs e )
{
//定义连接远程计算机的一些选项
ConnectionOptions options = new ConnectionOptions ( ) ;
options.Username = textBox2.Text ;
options.Password = textBox3.Text ;
ManagementScope scope = new ManagementScope( "\\\\" + textBox1.Text + "\\root\\cimv2", options ) ;
try
{
//用给定管理者用户名和口令连接远程的计算机
scope.Connect ( ) ;
System.Management.ObjectQuery oq = new System.Management.ObjectQuery ( "SELECT * FROM Win32_OperatingSystem" ) ;
ManagementObjectSearcher query1 = new ManagementObjectSearcher ( scope , oq ) ;
//得到WMI控制
ManagementObjectCollection queryCollection1 = query1.Get ( ) ;
foreach ( ManagementObject mo in queryCollection1 )
{
string [ ] ss= { "" } ;
//重启远程计算机
mo.InvokeMethod ( "Reboot" , ss ) ;
}
}
//报错
catch ( Exception ee )
{
MessageBox.Show ( "连接" + textBox1.Text + "出错,出错信息为:" + ee.Message ) ;
}
} private void Form1_Load(object sender, System.EventArgs e)
{
}
}
{
ConnectionOptions Options=new ConnectionOptions();
Options.Username=Ad.Text;
Options.Password=Pa.Text;
ManagementScope Scope=new ManagementScope("\\\\"+IP.Text+"\\root\\cimv2",Options); try
{
Scope.Connect(); ObjectQuery Oq=new ObjectQuery("SELECT * FROMWin32_OperatingSystem");
ManagementObjectSearcher Query = new ManagementObjectSearcher ( Scope , Oq ) ;
ManagementObjectCollection QueryCollection = Query.Get ( ) ; foreach ( ManagementObject MO in QueryCollection )
{
string [ ] ss= { "" } ;
MO.InvokeMethod ( "Reboot" , ss ) ;
}
}
catch(Exception er)
{
MessageBox.Show("连接" + IP.Text + "出错,出错信息为:" +er.Message);
}
}