先在添加引用中把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());
}
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());
}
解决方案 »
- response.redirect无法跳转
- VS2010 的项目, 按 F5 调试可以让程序启动, 但是生成解决方案后的 .exe 启动不了. 求解.
- .net打包安装把framework也一起打包的探讨?
- ``C#网页程序的文本限制输入```
- 请问在firefox中如何得到根节点的xml
- 请问下
- 程序执行时,我想用Label显示执行到哪一步,怎么让代码空闲一下把文字显示出来?
- 日期减去天数得到一个新的日期怎么做
- 求救!!!如何得到上传文件的扩展名呢?
- tooltip如何对treeview的每一个节点设置啊,很迷惑,不知道如何下手。(WinForm)
- 请问那有《C#入门经典》电子图书的下载
- C#中如何获得HWND(窗口句柄)?
请问如何在.net中安装.WMI这个名字空间