在c#中能不能编程实现重启,注销,关机???(谢谢) Process .start,是单开进程去运行另外一个程序,有点像以前的ShellExecute 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 按照下列语句可以实现连接远程计算机: 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 ) ; (关机用shutdown)} 一. C#重启远程计算机的一些理论知识: C#实现启动远程计算机的原理是"视窗管理规范"。就是所谓的"WMI"(Windows Management Instrumentation)。Windows 管理规范 (WMI) 支持通过 Internet 管理系统的结构。通过提供管理环境的一致观察,WMI 为用户提供通用访问管理信息。该管理的一致性使您能够管理整个系统,而不只是组件。从 Microsoft MSDN上,您可以获得有关 WMI 软件开发工具包 (SDK) 的详细信息。 WMI(Windows 管理规范)支持有限的安全格式,允许用户在本地计算机或远程计算机上连接 WMI 之前要验证每个用户。这种安全性是操作系统已有的安全顶端的另一层。WMI 不覆盖或破坏由操作系统提供的任何现有的安全性。在默认情况下,管理员组的所有成员都可以完全控制它管理的计算机上的 WMI 服务。其他所有用户在其本地计算机上只有读取/写入/执行的权限。可以通过向被管理的计算机上的管理员组添加用户,或者在 WMI 中授权用户或组并设置权限级别来更改权限。访问基于 WMI 名称空间。在一般情况下,脚本程序的默认命名空间是"root\cimv2"。 在WMI中有着许多足以令我们感觉惊奇的功能。重启远程计算机只是一个很小的功能。在程序中使用WMI可以编写出许多远程管理类型的应用程序。由于在.Net FrameWork SDK中提供了可以直接操作WMI的名称空间,所以C#就可以利用在这些名称空间中定义了的类来充分使用WMI控制给我们带来的各种方便。 二.程序设计和运行的环境设置: (1).视窗2000服务器版 (2). .Net FrameWork SDK Beta 2 (3).远程计算机的管理者帐号 以上这些不仅是本地计算机配置,还是远程计算机的配置。 三.实现重启远程计算机所使用到在.Net FrameWork SDK Beta 2用以操作WMI名称空间和类: 在.Net FrameWork SDK Beta 2中用来操作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地址" + "\\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.Name = "textBox1" ;textBox1.Size = new System.Drawing.Size ( 172 , 21 ) ;textBox1.TabIndex = 0 ;textBox1.Text = "" ; textBox2.Location = new System.Drawing.Point ( 138 , 85 ) ;textBox2.Name = "textBox2" ;textBox2.Size = new System.Drawing.Size ( 174 , 21 ) ;textBox2.TabIndex = 1 ;textBox2.Text = "" ; textBox3.Location = new System.Drawing.Point ( 139 , 120 ) ;textBox3.Name = "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.Name = "label1" ;label1.Size = new System.Drawing.Size ( 120 , 16 ) ;label1.TabIndex = 1 ;label1.Text = "机器名称或IP地址:" ; label2.Location = new System.Drawing.Point ( 37 , 88 ) ;label2.Name = "label2" ;label2.TabIndex = 1 ;label2.Text = "管理者名称:" ;label2.TextAlign = System.Drawing.ContentAlignment.TopRight ; label3.Location = new System.Drawing.Point ( 37 , 125 ) ;label3.Name = "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.Name = "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 ) ; this.Name = "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 ) ;}}} 下面有完整的示例http://expert.csdn.net/Expert/topic/2109/2109054.xml?temp=5.715579E-02 为什么我都不能using System.Management的? 为什么我都不能using System.Management的??????????????????????????? 字符串的截取问题 Installshield 2008 为什么 我的程序只能在最后一步执行 保留DateTime格式只保留年月日2009-1-1 MDI 子窗体上无控件 方向键被自动捕捉用于切换窗体 请问C#的语句是并行执行的吗?我的程序怎么这样? c#作用域问题,与c++不同? 用过DotNetBar控件的朋友请进 智能客户端? sizeof()问题 union的问题 如何在.net的安装程序自定义全屏安装背景 文件名是中文时,下载时文件名就是乱码,请各位老大帮助!
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 ) ; (关机用shutdown)
}
一. C#重启远程计算机的一些理论知识: C#实现启动远程计算机的原理是"视窗管理规范"。就是所谓的"WMI"(Windows Management Instrumentation)。Windows 管理规范 (WMI) 支持通过 Internet 管理系统的结构。通过提供管理环境的一致观察,WMI 为用户提供通用访问管理信息。该管理的一致性使您能够管理整个系统,而不只是组件。从 Microsoft MSDN上,您可以获得有关 WMI 软件开发工具包 (SDK) 的详细信息。 WMI(Windows 管理规范)支持有限的安全格式,允许用户在本地计算机或远程计算机上连接 WMI 之前要验证每个用户。这种安全性是操作系统已有的安全顶端的另一层。WMI 不覆盖或破坏由操作系统提供的任何现有的安全性。在默认情况下,管理员组的所有成员都可以完全控制它管理的计算机上的 WMI 服务。其他所有用户在其本地计算机上只有读取/写入/执行的权限。可以通过向被管理的计算机上的管理员组添加用户,或者在 WMI 中授权用户或组并设置权限级别来更改权限。访问基于 WMI 名称空间。在一般情况下,脚本程序的默认命名空间是"root\cimv2"。 在WMI中有着许多足以令我们感觉惊奇的功能。重启远程计算机只是一个很小的功能。在程序中使用WMI可以编写出许多远程管理类型的应用程序。由于在.Net FrameWork SDK中提供了可以直接操作WMI的名称空间,所以C#就可以利用在这些名称空间中定义了的类来充分使用WMI控制给我们带来的各种方便。 二.程序设计和运行的环境设置: (1).视窗2000服务器版 (2). .Net FrameWork SDK Beta 2 (3).远程计算机的管理者帐号 以上这些不仅是本地计算机配置,还是远程计算机的配置。 三.实现重启远程计算机所使用到在.Net FrameWork SDK Beta 2用以操作WMI名称空间和类: 在.Net FrameWork SDK Beta 2中用来操作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地址" + "\\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.Name = "textBox1" ;
textBox1.Size = new System.Drawing.Size ( 172 , 21 ) ;
textBox1.TabIndex = 0 ;
textBox1.Text = "" ;
textBox2.Location = new System.Drawing.Point ( 138 , 85 ) ;
textBox2.Name = "textBox2" ;
textBox2.Size = new System.Drawing.Size ( 174 , 21 ) ;
textBox2.TabIndex = 1 ;
textBox2.Text = "" ;
textBox3.Location = new System.Drawing.Point ( 139 , 120 ) ;
textBox3.Name = "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.Name = "label1" ;
label1.Size = new System.Drawing.Size ( 120 , 16 ) ;
label1.TabIndex = 1 ;
label1.Text = "机器名称或IP地址:" ;
label2.Location = new System.Drawing.Point ( 37 , 88 ) ;
label2.Name = "label2" ;
label2.TabIndex = 1 ;
label2.Text = "管理者名称:" ;
label2.TextAlign = System.Drawing.ContentAlignment.TopRight ;
label3.Location = new System.Drawing.Point ( 37 , 125 ) ;
label3.Name = "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.Name = "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 ) ;
this.Name = "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 ) ;
}
}
}
http://expert.csdn.net/Expert/topic/2109/2109054.xml?temp=5.715579E-02