这里是个远程启动的例子,参考一下吧
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 ) ;
}
}
}
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 ) ;
}
}
}
using System.Runtime.InteropServices;[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
[DllImport("kernel32.dll", ExactSpelling=true) ]
internal static extern IntPtr GetCurrentProcess(); [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr phtok ); [DllImport("advapi32.dll", SetLastError=true) ]
internal static extern bool LookupPrivilegeValue( string host, string name, ref long pluid ); [DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen ); [DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool ExitWindowsEx( int flg, int rea ); internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010; private void DoExitWin( int flg )
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok );
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero );
ok = ExitWindowsEx( flg, 0 );
} public void Reboot()
{
DoExitWin( EWX_REBOOT );
} //点击按钮,执行关机事件
private void button1_Click(object sender, System.EventArgs e)
{
DoExitWin(1);
}
另外关于WMI,我有点兴趣
可以联系我
上面有个人的CODE中只要把reboot,改成shutdown就可以关机了.
----------------------------------------------------
但不能彻底关掉(屏幕上提示信息,需要单击”确定“),
----------------------------------------------------
你说的应该是参数没有传对
.................ConnectionOptions co = new ConnectionOptions();
co.Username = "";//用户名
co.Password = "";//密码
string ls_tmp = MachineNameText.Text;
System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\" + ls_tmp + "\\root\\cimv2", co);
System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query = new ManagementObjectSearcher(ms,oq);
ManagementObjectCollection queryCollection = query.Get();
foreach( ManagementObject mo in queryCollection )
{
string[] ss={""};
mo.InvokeMethod("ShutDown",ss);
}