代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
namespace Ex18_11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//指定生成 WMI 连接所需的所有设置
ConnectionOptions op = new ConnectionOptions();
op.Username = "administrator";
//远程计算机用户名称
op.Password = "12345";
//远程计算机用户密码
//设置操作管理范围
ManagementScope scope = new ManagementScope("\\\\" + "121.35.174.154" + "\\root\\cimv2", op);
scope.Connect();
//将此 ManagementScope 连接到实际的 WMI 范围。
ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query = new ManagementObjectSearcher(scope, oq);
//得到WMI控制
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject obj in queryCollection)
{
obj.InvokeMethod("ShutDown", null); //执行关闭远程计算机,reboot为重新启动
}
}
}
}