C#远程关机代码:
//按钮点击事件
private void button1_Click(object sender, EventArgs e)
{
//此处需要添加System.Management的引用
ConnectionOptions op = new ConnectionOptions();
//获取远程计算机的用户名
op.Username = txtAdmin.Text.Trim();
//获取远程计算机的密码
op.Password = txtPassword.Text.Trim();
ManagementScope scope = new ManagementScope("" + textBox1.Text + "//root//cimv2", op);
scope.Connect();
ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query = new ManagementObjectSearcher(scope, oq);
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject obj in queryCollection)
{
obj.InvokeMethod("ShutDown", null); //命令换成reboot就是重启
}
}
此代码在VS2005中编写,操作系统为Window2000!