四.C#重启远程计算机的重要步骤和实现方法: 
   
    (1).连接远程计算机: 
   
    按照下列语句可以实现连接远程计算机: 
   

ConnectionOptions options = new ConnectionOptions ( ) ; 
   options.Username ="管理者帐号用户名"; 
   options.Password = "管理者帐号口令" ; 
   ManagementScope scope = new ManagementScope( "\\\\" + "远程计算机名或IP地址" + "\\root\\cimv2", options ) ; 
   //用给定管理者用户名和口令连接远程的计算机 
   scope.Connect ( ) ;


   
    (2).得到在远程计算机中可以进行WMI控制: 
   

System.Management.ObjectQuery oq = new System.Management.ObjectQuery ( "SELECT * FROM Win32_OperatingSystem" ) ; 
   ManagementObjectSearcher query1 = new ManagementObjectSearcher ( scope , oq ) ; 
   //得到WMI控制 
   ManagementObjectCollection queryCollection1 = query1.Get ( ) ;


   
    (3).调用WMI控制,实现重启远程计算机: 
   

foreach ( ManagementObject mo in queryCollection1 ) 
   { 
   string [ ] ss= { "" } ; 
   //重启远程计算机 
   mo.InvokeMethod ( "Reboot" , ss ) ; 
   }


   
    五.C#实现重启远程计算机的源程序代码(boot.cs)和执行界面: 
   
    在了解了C#实现重启远程计算机的这些重要步骤后,就可以从容的得到重启远程计算机的完整代码,具体如下: 
   

using System ; 
   using System.Drawing ; 
   using System.Collections ; 
   using System.ComponentModel ; 
   using System.Windows.Forms ; 
   using System.Data ; 
   using System.Management ; 
   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 ( ) 
   { 
   textBox1 = new TextBox ( ) ; 
   textBox2 = new TextBox ( ) ; 
   textBox3 = new TextBox ( ) ; 
   label1 = new Label ( ) ; 
   label2 = new Label ( ) ; 
   label3 = new Label ( ) ; 
   button1 = new Button ( ) ; 
    
   SuspendLayout ( ) ; 
   textBox1.Location = new System.Drawing.Point ( 140 , 46 ) ; 
    = "textBox1" ; 
   textBox1.Size = new System.Drawing.Size ( 172 , 21 ) ; 
   textBox1.TabIndex = 0 ; 
   textBox1.Text = "" ; 
    
   textBox2.Location = new System.Drawing.Point ( 138 , 85 ) ; 
    = "textBox2" ; 
   textBox2.Size = new System.Drawing.Size ( 174 , 21 ) ; 
   textBox2.TabIndex = 1 ; 
   textBox2.Text = "" ; 
    
   textBox3.Location = new System.Drawing.Point ( 139 , 120 ) ; 
    = "textBox3" ; 
   textBox3.PasswordChar = '*' ; 
   textBox3.Size = new System.Drawing.Size ( 173 , 21 ) ; 
   textBox3.TabIndex = 2 ; 
   textBox3.Text = "" ; 
    
   label1.Location = new System.Drawing.Point ( 24 , 50 ) ; 
    = "label1" ; 
   label1.Size = new System.Drawing.Size ( 120 , 16 ) ; 
   label1.TabIndex = 1 ; 
   label1.Text = "机器名称或IP地址:" ; 
    
   label2.Location = new System.Drawing.Point ( 37 , 88 ) ; 
    = "label2" ; 
   label2.TabIndex = 1 ; 
   label2.Text = "管理者名称:" ; 
   label2.TextAlign = System.Drawing.ContentAlignment.TopRight ; 
    
   label3.Location = new System.Drawing.Point ( 37 , 125 ) ; 
    = "label3" ; 
   label3.Size = new System.Drawing.Size ( 100 , 16 ) ; 
   label3.TabIndex = 1 ; 
   label3.Text = "管理者密码:" ; 
   label3.TextAlign = System.Drawing.ContentAlignment.TopRight ; 
    
   button1.Location = new System.Drawing.Point ( 95 , 168 ) ; 
    = "button1" ; 
   button1.Size = new System.Drawing.Size ( 136 , 32 ) ; 
   button1.TabIndex = 3 ; 
   button1.Text = "重新启动远程计算机" ; 
   button1.Click += new System.EventHandler ( button1_Click ) ; 
    
   this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ; 
   this.ClientSize = new System.Drawing.Size ( 336 , 245 ) ; 
   this.Controls.Add ( button1 ) ; 
   this.Controls.Add ( textBox1 ) ; 
   this.Controls.Add ( textBox2 ) ; 
   this.Controls.Add ( textBox3 ) ; 
   this.Controls.Add ( label1 ) ; 
   this.Controls.Add ( label2 ) ; 
   this.Controls.Add ( label3 ) ; 
    
    = "Form1" ; 
   this.Text = "利用C#重新启动远程计算机" ; 
   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 ) ; 
   } 
   } 
   }


   
    下图是编译上面代码后得到的程序运行界面: 
   
   
   
  图01:用C#实现重启远程计算机的程序运行界面 
   
    六.总结: 
   
    其实WMI控制可以实现很多以前让我们很头痛的操作。并且使用WMI编写的管理程序也比不用WMI来实现同样功能的程序在设计难度上大大减轻。WMI内容十分丰富,重新启动远程计算机只是其中的一个最为基本的操作。在使用WMI控制之前有一点必须记住,就是你必须知道你所要进行操作的远程计算机的超级管理者的帐号,这是使用WMI的一个前提。