C#重启远程计算机的代码

2016-02-19 11:14 8 1 收藏

只要你有一台电脑或者手机,都能关注图老师为大家精心推荐的C#重启远程计算机的代码,手机电脑控们准备好了吗?一起看过来吧!

【 tulaoshi.com - 编程语言 】

如果叫你实现远程启动别人的计算机,你首先想到的可能是先做一个在远程计算机上面运行客户端程序,然后在本地计算机上面再做一个服务器端程序,通过这二个程序直接的通讯实现重启远程计算机。这当然是一个方法。但这未免有点麻烦。如果现在只告诉你远程计算机的管理者的登陆帐号,而并不允许你在远程的计算机上面运行一个所谓的客户端程序,让你通过程序来完成重启远程计算机。不知道你是否感觉有些困难了。其实按照上面的这些条件实现重启远程计算机,利用C#可以比较方便的完成。下面就来介绍一下具体的实现方法。 
一. C#重启远程计算机的一些理论知识: 
  C#实现启动远程计算机的原理是"视窗管理规范"。就是所谓的"WMI"(Windows Management Instrumentation)。Windows 管理规范 (WMI) 支持通过 Internet 管理系统的结构。通过提供管理环境的一致观察,WMI 为用户提供通用访问管理信息。该管理的一致性使您能够管理整个系统,而不只是组件。从 Microsoft MSDN上,您可以获得有关 WMI 软件开发工具包 (SDK) 的详细信息。 
  WMI(Windows 管理规范)支持有限的安全格式,允许用户在本地计算机或远程计算机上连接 WMI 之前要验证每个用户。这种安全性是操作系统已有的安全顶端的另一层。WMI 不覆盖或破坏由操作系统提供的任何现有的安全性。在默认情况下,管理员组的所有成员都可以完全控制它管理的计算机上的 WMI 服务。其他所有用户在其本地计算机上只有读取/写入/执行的权限。可以通过向被管理的计算机上的管理员组添加用户,或者在 WMI 中授权用户或组并设置权限级别来更改权限。访问基于 WMI 名称空间。在一般情况下,脚本程序的默认命名空间是"rootcimv2"。 
  在WMI中有着许多足以令我们感觉惊奇的功能。重启远程计算机只是一个很小的功能。在程序中使用WMI可以编写出许多远程管理类型的应用程序。由于在.Net FrameWork SDK中提供了可以直接操作WMI的名称空间,所以C#就可以利用在这些名称空间中定义了的类来充分使用WMI控制给我们带来的各种方便。 
二.程序设计和运行的环境设置: 
  (1).windows 2000 Professional 
  (2). .Net FrameWork SDK 
  (3).远程计算机的管理者帐号 
  以上这些不仅是本地计算机配置,还是远程计算机的配置。 
三.实现重启远程计算机所使用到在.Net FrameWork SDK用以操作WMI名称空间和类: 
  添加引用System.Management; 
  在.Net FrameWork SDK中用来操作WMI的名称空间主要是"System.Management"。要实现重启远程计算机所使用到的类主要有六个: 
  . "ConnectionOptions"类主要定义远程计算机的管理员帐号; 
  . "ManagementScope"主要是以给定的管理员帐号连接给定计算机名或者IP地址的计算机; 
  . "ObjectQuery"类功能是定义对远程计算机要实现那些地远程操作; 
  . "ManagementObjectSearcher"类从已经完成远程连接的计算机中,得到有那些WMI操作; 
  . "ManagementObjectCollection"类存放得到WMI操作; 
  . "ManagementObject"类调用远程计算机可进行WMI操作。 
  在本文介绍的操作就是重启操作。 
四.C#重启远程计算机的重要步骤和实现方法: 
  (1).连接远程计算机: 
  按照下列语句可以实现连接远程计算机: 
ConnectionOptions options = new ConnectionOptions ( ) ; 
options.Username ="管理者帐号用户名"; 
options.Password = "管理者帐号口令" ; 
ManagementScope scope = new ManagementScope( "\" + "远程计算机名或IP地址" + "rootcimv2", 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; 
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 
/// 必需的设计器变量。 
/// /summary 
private System.ComponentModel.Container components = null; 
public Form1() 

// 
// Windows 窗体设计器支持所必需的 
// 
InitializeComponent(); 
// 
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码 
// 

/// 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 
/// 设计器支持所需的方法 - 不要使用代码编辑器修改 
/// 此方法的内容。 
/// /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 = "远程计算机名或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 
/// summary 
/// 应用程序的主入口点。 
/// /summary 
[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+"rootcimv2",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); 



}  

来源:https://www.tulaoshi.com/n/20160219/1596811.html

延伸阅读
标签: 电脑入门
要进入BIOS设置,在系统启动时敲DEL键(或启动时第一屏上提示的键,通常的提示是:Press Delete to setup,或其他类似的句子)。如果的BIOS不能访问或者你不能进入CMOS设置,你就需要下载TweakBIOS,该软件不但会使你能极为简便地访 问CMOS,还能使你可以访问一些你以前不能访问的COMS设置项。 我们首先来看默认设置中被打开,但却被人工修改...
以远程计算机上的用户身份访问Com+应用 DELPHI程序员开发com+应用的速度是非常快的,其主要原因是其较好地封装了com+的windows底层功能,开发人员通过较为简单的类继承就避开了复杂的com+底层技术细节,使开发人员将精力放在应用本身的功能上面。Delphi在封装com+应用时采取了许多折衷,在保留通用性的同时也避开了一些实现起来困难但是应...
标签: 电脑入门
我们在用电脑时,电脑开着有时候会短暂的离开下电脑,而电脑上有些东西不想让其他的看到或是操作。可以设置一下安全保护方法。暂时锁定计算机。 1.Win+L键法 在WindowsXP中在任何时候按下Win+L(L 是LockStation之意)键都可以锁定计算机。这个 锁定计算机的快捷键 现在Win7中也同样有用。大家可以试试,按下.Win+L这个锁定计算机的快捷键,...
假如安装完Oracle后,发现计算机名不符合要求,可以先修改计算机名,然后在net assistant中,修改监听的计算机名,也可以通过(listener.ora和tnsnames.ora的host改成现计算机名)然后把所有network/agent/下的.q文件删除,重新启动就可以了。
摘要 远程执行是C#中一种使开发人员能够使用远程对象的基础架构。远程对象是一种位于调用者应用域之外的对象。本文中的例子说明了如何使用二种远程对象访问机制(值传递和地址传递),它还通过一个简单的、功能强大的任务服务器的实现说明了分布式计算中远程对象的强大功能。 任务服务器能够接受所有能实现ITask界面的对象,并在...

经验教程

264

收藏

42
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部