两个人做人爱视频免费,97久久精品人人搡人妻人人玩,欧洲精品码一区二区三区,999zyz玖玖资源站永久

我要投稿 投訴建議

C#實現遠程重啟計算機的方法

時間:2021-04-03 14:47:59 計算機等級 我要投稿

C#實現遠程重啟計算機的方法

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

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

  C#實現啟動遠程計算機的原理是"視窗管理規范"。就是所謂的"WMI"(WindowsManagementInstrumentation)。Windows管理規范(WMI)支持通過Internet管理系統的結構。通過提供管理環境的一致觀察,WMI為用戶提供通用訪問管理信息。該管理的.一致性使您能夠管理整個系統,而不只是組件。從MicrosoftMSDN上,您可以獲得有關WMI軟件開發工具包(SDK)的詳細信息。

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

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

  二.程序設計和運行的環境設置:

  (1).windows2000Professional

  (2)..NetFrameWorkSDK

  (3).遠程計算機的管理者帳號

  以上這些不僅是本地計算機配置,還是遠程計算機的配置。

  三.實現重啟遠程計算機所使用到在.NetFrameWorkSDK用以操作WMI名稱空間和類:

  添加引用System.Management;

  在.NetFrameWorkSDK中用來操作WMI的名稱空間主要是"System.Management"。要實現重啟遠程計算機所使用到的類主要有六個:

  ."ConnectionOptions"類主要定義遠程計算機的管理員帳號;

  ."ManagementScope"主要是以給定的管理員帳號連接給定計算機名或者IP地址的計算機;

  ."ObjectQuery"類功能是定義對遠程計算機要實現那些地遠程操作;

  ."ManagementObjectSearcher"類從已經完成遠程連接的計算機中,得到有那些WMI操作;

  ."ManagementObjectCollection"類存放得到WMI操作;

  ."ManagementObject"類調用遠程計算機可進行WMI操作。

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

  四.C#重啟遠程計算機的重要步驟和實現方法:

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

  按照下列語句可以實現連接遠程計算機:

  ConnectionOptionsoptions=newConnectionOptions();

  options.Username="管理者帳號用戶名";

  options.Password="管理者帳號口令";

  ManagementScopescope=newManagementScope(""+"遠程計算機名或IP地址"+"rootcimv2",options);

  //用給定管理者用戶名和口令連接遠程的計算機

  scope.Connect();

  (2).得到在遠程計算機中可以進行WMI控制:

  System.Management.ObjectQueryoq=newSystem.Management.ObjectQuery("SELECT*FROMWin32_OperatingSystem");

  ManagementObjectSearcherquery1=newManagementObjectSearcher(scope,oq);

  //得到WMI控制

  ManagementObjectCollectionqueryCollection1=query1.Get();

  (3).調用WMI控制,實現重啟遠程計算機:

  foreach(ManagementObjectmoinqueryCollection1)

  {

  string[]ss={""};

  //重啟遠程計算機

  mo.InvokeMethod("Reboot",ss);

  }

  五.C#實現重啟遠程計算機的源程序代碼(boot.cs)和執行界面:

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

  usingSystem;

  usingSystem.Drawing;

  usingSystem.Collections;

  usingSystem.ComponentModel;

  usingSystem.Windows.Forms;

  usingSystem.Data;

  usingSystem.Management;

  namespaceReStartboot

  {

  ///


  ///Form1的摘要說明。

  ///

  publicclassForm1:System.Windows.Forms.Form

  {

  privateSystem.Drawing.Printing.PrintDocumentprintDocument1;

  privateSystem.Windows.Forms.Labellabel1;

  privateSystem.Windows.Forms.Labellabel2;

  privateSystem.Windows.Forms.Labellabel3;

  privateSystem.Windows.Forms.TextBoxtextBox1;

  privateSystem.Windows.Forms.TextBoxtextBox2;

  privateSystem.Windows.Forms.TextBoxtextBox3;

  privateSystem.Windows.Forms.Buttonbutton1;

  ///


  ///必需的設計器變量。

  ///

  privateSystem.ComponentModel.Containercomponents=null;

  publicForm1()

  {

  //

  //Windows窗體設計器支持所必需的

  //

  InitializeComponent();

  //

  //TODO:在InitializeComponent調用后添加任何構造函數代碼

  //

  }

  ///


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

  ///

  protectedoverridevoidDispose(booldisposing)

  {

  if(disposing)

  {

  if(components!=null)

  {

  components.Dispose();

  }

  }

  base.Dispose(disposing);

  }

  #regionWindowsFormDesignergeneratedcode

  ///


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

  ///此方法的內容。

  ///

  privatevoidInitializeComponent()

  {

  this.printDocument1=newSystem.Drawing.Printing.PrintDocument();

  this.label1=newSystem.Windows.Forms.Label();

  this.label2=newSystem.Windows.Forms.Label();

  this.label3=newSystem.Windows.Forms.Label();

  this.textBox1=newSystem.Windows.Forms.TextBox();

  this.textBox2=newSystem.Windows.Forms.TextBox();

  this.textBox3=newSystem.Windows.Forms.TextBox();

  this.button1=newSystem.Windows.Forms.Button();

  this.SuspendLayout();

  //

  //label1

  //

  this.label1.Location=newSystem.Drawing.Point(16,32);

  this.label1.Name="label1";

  this.label1.Size=newSystem.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=newSystem.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=newSystem.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=newSystem.Drawing.Point(136,32);

  this.textBox1.Name="textBox1";

  this.textBox1.Size=newSystem.Drawing.Size(152,21);

  this.textBox1.TabIndex=3;

  this.textBox1.Text="";

  //

  //textBox2

  //

  this.textBox2.Location=newSystem.Drawing.Point(136,80);

  this.textBox2.Name="textBox2";

  this.textBox2.Size=newSystem.Drawing.Size(152,21);

  this.textBox2.TabIndex=4;

  this.textBox2.Text="";

  //

  //textBox3

  //

  this.textBox3.Location=newSystem.Drawing.Point(136,128);

  this.textBox3.Name="textBox3";

  this.textBox3.Size=newSystem.Drawing.Size(152,21);

  this.textBox3.TabIndex=5;

  this.textBox3.Text="";

  //

  //button1

  //

  this.button1.Location=newSystem.Drawing.Point(104,168);

  this.button1.Name="button1";

  this.button1.Size=newSystem.Drawing.Size(120,23);

  this.button1.TabIndex=6;

  this.button1.Text="重啟遠程計算機";

  this.button1.Click+=newSystem.EventHandler(this.button1_Click);

  //

  //Form1

  //

  this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);

  this.ClientSize=newSystem.Drawing.Size(320,213);

  this.Controls.AddRange(newSystem.Windows.Forms.Control[]{this.label1});

  this.Name="Form1";

  this.Text="重啟遠程計算機";

  this.ResumeLayout(false);

  }

  #endregion

  ///


  ///應用程序的主入口點。

  ///

  [STAThread]

  staticvoidMain()

  {

  Application.Run(newForm1());

  }

  privatevoidbutton1_Click(objectsender,System.EventArgse)

  {

  //定義連接遠程計算機的一些選項

  ConnectionOptionsoptions=newConnectionOptions();

  options.Username=textBox2.Text;

  options.Password=textBox3.Text;

  ManagementScopescope=newManagementScope(""+textBox1.Text+"rootcimv2",options);

  try

  {

  //用給定管理者用戶名和口令連接遠程的計算機

  scope.Connect();

  ObjectQueryoq=newObjectQuery("select*fromwin32_OperatingSystem");

  ManagementObjectSearcherquery1=newManagementObjectSearcher(scope,oq);

  ManagementObjectCollectionqueryCollection1=query1.Get();

  foreach(ManagementObjectmoinqueryCollection1)

  {

  string[]ss={""};

  mo.InvokeMethod("Reboot",ss);

  }

  }

  catch(Exceptioner)

  {

  MessageBox.Show("連接"+textBox1.Text+"出錯,出錯信息為:"+er.Message);

  }

  }

  }

  }

【C#實現遠程重啟計算機的方法】相關文章:

運行多臺遠程計算機的具體方法11-25

探討計算機基礎課程遠程教學系統的設計及實現12-15

計算機連接不到遠程桌面的解放方法11-23

計算機技能:C#基礎之數據類型轉換12-08

C#基礎的面試試題收集06-05

關于關閉計算機的方法11-27

計算機故障修復的方法11-20

C#面試試題附答案06-21

為什么電腦會藍屏重啟11-29

主站蜘蛛池模板: 竹山县| 嵊州市| 林甸县| 巴林右旗| 岳西县| 桦南县| 大竹县| 晋宁县| 福海县| 苍溪县| 甘洛县| 荥阳市| 安化县| 达州市| 龙口市| 内乡县| 连江县| 楚雄市| 饶平县| 永善县| 瓮安县| 开江县| 滁州市| 沿河| 新营市| 吐鲁番市| 湄潭县| 蓬安县| 唐河县| 黑龙江省| 吉林省| 迁西县| 称多县| 北海市| 龙海市| 大名县| 日喀则市| 梁平县| 常熟市| 文安县| 云南省|