日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使用C#實現(xiàn)遠程啟動之原理及方法

如果叫你實現(xiàn)遠程啟動別人的計算機,你首先想到的可能是先做一個在遠程計算機上面運行客戶端程序,然后在本地計算機上面再做一個服務(wù)器端 程序,通過這二個程序直接的通訊實現(xiàn)重啟遠程計算機。這當然是一個方法。但這未免有點麻煩。如果現(xiàn)在只告訴你遠程計算機的管理者的登陸帳號,而并不允許你在遠程的計算機上面運行一個所謂的客戶端程序,讓你通過程序來完成重啟遠程計算機。不知道你是否感覺有些困難了。其實按照上面的這些條件實現(xiàn)重啟遠程計算機,利用C??梢员容^方便的完成。下面就來介紹一下具體的實現(xiàn)方法。

1.C#重啟遠程計算機的一些理論知識:

C#實現(xiàn)啟動遠程計算機的原理是"視窗管理規(guī)范"。就是所謂的"WMI"(Windows Management Instrumentation)。Windows 管理規(guī)范 (WMI) 支持通過 Internet 管理系統(tǒng)的結(jié)構(gòu)。通過提供管理環(huán)境的一致觀察,WMI 為用戶提供通用訪問管理信息。該管理的一致性使您能夠管理整個系統(tǒng),而不只是組件。從 Microsoft MSDN上,您可以獲得有關(guān) WMI 軟件開發(fā)工具包 (SDK) 的詳細信息。

WMI(Windows 管理規(guī)范)支持有限的安全格式,允許用戶在本地計算機或遠程計算機上連接 WMI 之前要驗證每個用戶。這種安全性 是操作系統(tǒng)已有的安全頂端的另一層。WMI 不覆蓋或破壞由操作系統(tǒng)提供的任何現(xiàn)有的安全性。在默認情況下,管理員組的所有成員都可以完全控制它管理的計算機上的 WMI 服務(wù)。其他所有用戶在其本地計算機上只有讀取/寫入/執(zhí)行的權(quán)限??梢酝ㄟ^向被管理的計算機上的管理員組添加用戶,或者在 WMI 中授權(quán)用戶或組并設(shè)置權(quán)限級別來更改權(quán)限。訪問基于 WMI 名稱空間。在一般情況下,腳本程序的默認命名空間是"root\cimv2"。

在WMI中有著許多足以令我們感覺驚奇的功能。重啟遠程計算機只是一個很小的功能。在程序中使用WMI可以編寫出許多遠程管理類型的應(yīng)用程序。由于在.Net FrameWork SDK中提供了可以直接操作WMI的名稱空間,所以C#就可以利用在這些名稱空間中定義了的類來充分使用WMI控制給我們帶來的各種方便。

2.程序設(shè)計和運行的環(huán)境設(shè)置:
(1).windows 2000 Professional
(2). .Net FrameWork SDK
(3).遠程計算機的管理者帳號
以上這些不僅是本地計算機配置,還是遠程計算機的配置。

3.實現(xiàn)重啟遠程計算機所使用到在.Net FrameWork SDK用以操作WMI名稱空間和類:

添加引用System.Management;

在.Net FrameWork SDK中用來操作WMI的名稱空間主要是"System.Management"。要實現(xiàn)重啟遠程計算機所使用到的類主要有六個:
· "ConneCTionOptions"類主要定義遠程計算機的管理員帳號;
·"ManagementScope"主要是以給定的管理員帳號連接給定計算機名或者IP地址的計算機;
·"ObjectQuery"類功能是定義對遠程計算機要實現(xiàn)那些地遠程操作;
·"ManagementObjectSearcher"類從已經(jīng)完成遠程連接的計算機中,得到有那些WMI操作;
·"ManagementObjectCollection"類存放得到WMI操作;
·"ManagementObject"類調(diào)用遠程計算機可進行WMI操作。

在本文介紹的操作就是重啟操作。

4.C#重啟遠程計算機的重要步驟和實現(xiàn)方法:

(1).連接遠程計算機:

按照下列語句可以實現(xiàn)連接遠程計算機:

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).調(diào)用WMI控制,實現(xiàn)重啟遠程計算機:

foreach ( ManagementObject mo in queryCollection1 ) 
{
string [ ] ss= { "" } ;
//重啟遠程計算機
mo.InvokeMethod ( "Reboot" , ss ) ;
}

5.C#實現(xiàn)重啟遠程計算機的源程序代碼(boot.cs)和執(zhí)行界面:

在了解了C#實現(xiàn)重啟遠程計算機的這些重要步驟后,就可以從容的得到重啟遠程計算機的完整代碼,具體如下:

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Management;

namespace ReStartboot

{

///

/// 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;

///

/// 必需的設(shè)計器變量。

///

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Windows 窗體設(shè)計器支持所必需的

//

InitializeComponent();

//

// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼

//

}

///

/// 清理所有正在使用的資源。

///

protected override void Dispose(bool disposing)

{

if (disposing)

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose(disposing);

}

#region Windows Form Designer generated code

///

/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改

/// 此方法的內(nèi)容。

///

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 = "遠程計算機名或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 = "重啟遠程計算機";

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 = "重啟遠程計算機";

this.ResumeLayout(false);

}

#endregion

///

/// 應(yīng)用程序的主入口點。

///

[STAThread]

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();

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 + "出錯,出錯信息為:" + er.Message);

}

}

}

}


當前文章:使用C#實現(xiàn)遠程啟動之原理及方法
URL地址:http://www.dlmjj.cn/article/djggspj.html