亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#重啟遠(yuǎn)程計(jì)算機(jī)的代碼

 更新時(shí)間:2007年04月16日 00:00:00   作者:  
如果叫你實(shí)現(xiàn)遠(yuǎn)程啟動(dòng)別人的計(jì)算機(jī),你首先想到的可能是先做一個(gè)在遠(yuǎn)程計(jì)算機(jī)上面運(yùn)行客戶端程序,然后在本地計(jì)算機(jī)上面再做一個(gè)服務(wù)器端程序,通過這二個(gè)程序直接的通訊實(shí)現(xiàn)重啟遠(yuǎn)程計(jì)算機(jī)。這當(dāng)然是一個(gè)方法。但這未免有點(diǎn)麻煩。如果現(xiàn)在只告訴你遠(yuǎn)程計(jì)算機(jī)的管理者的登陸帳號(hào),而并不允許你在遠(yuǎn)程的計(jì)算機(jī)上面運(yùn)行一個(gè)所謂的客戶端程序,讓你通過程序來完成重啟遠(yuǎn)程計(jì)算機(jī)。不知道你是否感覺有些困難了。其實(shí)按照上面的這些條件實(shí)現(xiàn)重啟遠(yuǎn)程計(jì)算機(jī),利用C??梢员容^方便的完成。下面就來介紹一下具體的實(shí)現(xiàn)方法。 
一. C#重啟遠(yuǎn)程計(jì)算機(jī)的一些理論知識(shí): 
  C#實(shí)現(xiàn)啟動(dòng)遠(yuǎn)程計(jì)算機(jī)的原理是"視窗管理規(guī)范"。就是所謂的"WMI"(Windows Management Instrumentation)。Windows 管理規(guī)范 (WMI) 支持通過 Internet 管理系統(tǒng)的結(jié)構(gòu)。通過提供管理環(huán)境的一致觀察,WMI 為用戶提供通用訪問管理信息。該管理的一致性使您能夠管理整個(gè)系統(tǒng),而不只是組件。從 Microsoft MSDN上,您可以獲得有關(guān) WMI 軟件開發(fā)工具包 (SDK) 的詳細(xì)信息。 
  WMI(Windows 管理規(guī)范)支持有限的安全格式,允許用戶在本地計(jì)算機(jī)或遠(yuǎn)程計(jì)算機(jī)上連接 WMI 之前要驗(yàn)證每個(gè)用戶。這種安全性是操作系統(tǒng)已有的安全頂端的另一層。WMI 不覆蓋或破壞由操作系統(tǒng)提供的任何現(xiàn)有的安全性。在默認(rèn)情況下,管理員組的所有成員都可以完全控制它管理的計(jì)算機(jī)上的 WMI 服務(wù)。其他所有用戶在其本地計(jì)算機(jī)上只有讀取/寫入/執(zhí)行的權(quán)限??梢酝ㄟ^向被管理的計(jì)算機(jī)上的管理員組添加用戶,或者在 WMI 中授權(quán)用戶或組并設(shè)置權(quán)限級(jí)別來更改權(quán)限。訪問基于 WMI 名稱空間。在一般情況下,腳本程序的默認(rèn)命名空間是"root\cimv2"。 
  在WMI中有著許多足以令我們感覺驚奇的功能。重啟遠(yuǎn)程計(jì)算機(jī)只是一個(gè)很小的功能。在程序中使用WMI可以編寫出許多遠(yuǎn)程管理類型的應(yīng)用程序。由于在.Net FrameWork SDK中提供了可以直接操作WMI的名稱空間,所以C#就可以利用在這些名稱空間中定義了的類來充分使用WMI控制給我們帶來的各種方便。 
二.程序設(shè)計(jì)和運(yùn)行的環(huán)境設(shè)置: 
 ?。?).windows 2000 Professional 
  (2). .Net FrameWork SDK 
 ?。?).遠(yuǎn)程計(jì)算機(jī)的管理者帳號(hào) 
  以上這些不僅是本地計(jì)算機(jī)配置,還是遠(yuǎn)程計(jì)算機(jī)的配置。 
三.實(shí)現(xiàn)重啟遠(yuǎn)程計(jì)算機(jī)所使用到在.Net FrameWork SDK用以操作WMI名稱空間和類: 
  添加引用System.Management; 
  在.Net FrameWork SDK中用來操作WMI的名稱空間主要是"System.Management"。要實(shí)現(xiàn)重啟遠(yuǎn)程計(jì)算機(jī)所使用到的類主要有六個(gè): 
  . "ConnectionOptions"類主要定義遠(yuǎn)程計(jì)算機(jī)的管理員帳號(hào); 
  . "ManagementScope"主要是以給定的管理員帳號(hào)連接給定計(jì)算機(jī)名或者IP地址的計(jì)算機(jī); 
  . "ObjectQuery"類功能是定義對(duì)遠(yuǎn)程計(jì)算機(jī)要實(shí)現(xiàn)那些地遠(yuǎn)程操作; 
  . "ManagementObjectSearcher"類從已經(jīng)完成遠(yuǎn)程連接的計(jì)算機(jī)中,得到有那些WMI操作; 
  . "ManagementObjectCollection"類存放得到WMI操作; 
  . "ManagementObject"類調(diào)用遠(yuǎn)程計(jì)算機(jī)可進(jìn)行WMI操作。 
  在本文介紹的操作就是重啟操作。 
四.C#重啟遠(yuǎn)程計(jì)算機(jī)的重要步驟和實(shí)現(xiàn)方法: 
 ?。?).連接遠(yuǎn)程計(jì)算機(jī): 
  按照下列語句可以實(shí)現(xiàn)連接遠(yuǎn)程計(jì)算機(jī): 
ConnectionOptions options = new ConnectionOptions ( ) ; 
options.Username ="管理者帳號(hào)用戶名"; 
options.Password = "管理者帳號(hào)口令" ; 
ManagementScope scope = new ManagementScope( "\\\\" + "遠(yuǎn)程計(jì)算機(jī)名或IP地址" + "\\root\\cimv2", options ) ; 
//用給定管理者用戶名和口令連接遠(yuǎn)程的計(jì)算機(jī) 
scope.Connect ( ) ; 
 ?。?).得到在遠(yuǎn)程計(jì)算機(jī)中可以進(jìn)行WMI控制: 
System.Management.ObjectQuery oq = new System.Management.ObjectQuery ( "SELECT * FROM Win32_OperatingSystem" ) ; 
ManagementObjectSearcher query1 = new ManagementObjectSearcher ( scope , oq ) ; 
//得到WMI控制 
ManagementObjectCollection queryCollection1 = query1.Get ( ) ; 
 ?。?).調(diào)用WMI控制,實(shí)現(xiàn)重啟遠(yuǎn)程計(jì)算機(jī): 
foreach ( ManagementObject mo in queryCollection1 ) 

string [ ] ss= { "" } ; 
//重啟遠(yuǎn)程計(jì)算機(jī) 
mo.InvokeMethod ( "Reboot" , ss ) ; 

五.C#實(shí)現(xiàn)重啟遠(yuǎn)程計(jì)算機(jī)的源程序代碼(boot.cs)和執(zhí)行界面: 
  在了解了C#實(shí)現(xiàn)重啟遠(yuǎn)程計(jì)算機(jī)的這些重要步驟后,就可以從容的得到重啟遠(yuǎn)程計(jì)算機(jī)的完整代碼,具體如下: 
using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
using System.Management; 
namespace ReStartboot 

/// <summary> 
/// 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> 
/// 必需的設(shè)計(jì)器變量。 
/// </summary> 
private System.ComponentModel.Container components = null; 
public Form1() 

// 
// Windows 窗體設(shè)計(jì)器支持所必需的 
// 
InitializeComponent(); 
// 
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼 
// 

/// <summary> 
/// 清理所有正在使用的資源。 
/// </summary> 
protected override void Dispose( bool disposing ) 

if( disposing ) 

if (components != null) 

components.Dispose(); 


base.Dispose( disposing ); 

#region Windows Form Designer generated code 
/// <summary> 
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改 
/// 此方法的內(nèi)容。 
/// </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 = "遠(yuǎn)程計(jì)算機(jī)名或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 = "管理員名:"; 
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 = "密碼:"; 
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 = "重啟遠(yuǎn)程計(jì)算機(jī)"; 
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 = "重啟遠(yuǎn)程計(jì)算機(jī)"; 
this.ResumeLayout(false); 

#endregion 
/// <summary> 
/// 應(yīng)用程序的主入口點(diǎn)。 
/// </summary> 
[STAThread] 
static void Main() 

Application.Run(new Form1()); 

private void button1_Click(object sender, System.EventArgs e) 

//定義連接遠(yuǎn)程計(jì)算機(jī)的一些選項(xiàng) 
ConnectionOptions options=new ConnectionOptions(); 
options.Username=textBox2.Text; 
options.Password=textBox3.Text; 
ManagementScope scope=new ManagementScope("\\\\"+textBox1.Text+"\\root\\cimv2",options); 
try 

//用給定管理者用戶名和口令連接遠(yuǎn)程的計(jì)算機(jī) 
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("連接" + textBox1.Text + "出錯(cuò),出錯(cuò)信息為:" +er.Message); 



}  

相關(guān)文章

最新評(píng)論