先在添加引用中把System.Management对象引用,使用ConnectionOptions对象连接计算机ConnectionOptions options = new ConnectionOptions();
options.Username = this.textBox2.Text;
options.Password = this.textBox3.Text;
try
{
ManagementScope conn= new ManagementScope("\\\\"+this.textBox1.Text+"\\root\\cimv2",options);
conn.Connect();
ObjectQuery oq = new ObjectQuery("select * from Win32_OperatingSystem");//操作信息 ManagementObjectSearcher query1 = new ManagementObjectSearcher(conn,oq);
ManagementObjectCollection queryCollection = query1.Get();
foreach(ManagementObject mo in queryCollection)
{
string[] ss = {""};
if(this.comboBox1.Text == "重新启动")
mo.InvokeMethod("Reboot",ss);
else if (this.comboBox1.Text == "远程关机")
mo.InvokeMethod("Shutdown",ss);
else
MessageBox.Show("请选择操作!");
}
}
catch(System.UnauthorizedAccessException error)
{
                MessageBox.Show("连接"+this.textBox1.Text +"出错,出错信息:"+error.Message.ToString());
}
catch(Exception err)
{
MessageBox.Show("连接"+this.textBox1.Text +"出错,出错信息:"+err.Message.ToString());
}