/// <summary>
/// 关机
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
//指定生成 WMI 连接所需的所有设置
ConnectionOptions op = new ConnectionOptions();
op.Username =txtUserName.Text.Trim(); //远程计算机用户名称
op.Password = txtPwd.Text.Trim(); //远程计算机用户密码
string str = " " + txtIP.Text.Trim() + " //root//cimv2";
//设置操作管理范围
ManagementScope scope = new ManagementScope(str, op);
scope.Connect(); //将此 ManagementScope 连接到实际的 WMI 范围。
ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query = new ManagementObjectSearcher(scope, oq);
//得到WMI控制
ManagementObjectCollection queryCollection = query.Get();
try
{
foreach (ManagementObject obj in queryCollection)
{
obj.InvokeMethod("ShutDown",null); //执行关闭远程计算机,reboot为重新启动
}
MessageBox.Show("关机成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 重启
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
//指定生成 WMI 连接所需的所有设置
ConnectionOptions op = new ConnectionOptions();
op.Username = txtUserName.Text.Trim(); //远程计算机用户名称
op.Password = txtPwd.Text.Trim(); //远程计算机用户密码
string str = " " + txtIP.Text.Trim() + " //root//cimv2";
//设置操作管理范围
ManagementScope scope = new ManagementScope(str, 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("Reboot", null); //执行关闭远程计算机,reboot为重新启动
}
}
/// 关机
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
//指定生成 WMI 连接所需的所有设置
ConnectionOptions op = new ConnectionOptions();
op.Username =txtUserName.Text.Trim(); //远程计算机用户名称
op.Password = txtPwd.Text.Trim(); //远程计算机用户密码
string str = " " + txtIP.Text.Trim() + " //root//cimv2";
//设置操作管理范围
ManagementScope scope = new ManagementScope(str, op);
scope.Connect(); //将此 ManagementScope 连接到实际的 WMI 范围。
ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query = new ManagementObjectSearcher(scope, oq);
//得到WMI控制
ManagementObjectCollection queryCollection = query.Get();
try
{
foreach (ManagementObject obj in queryCollection)
{
obj.InvokeMethod("ShutDown",null); //执行关闭远程计算机,reboot为重新启动
}
MessageBox.Show("关机成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 重启
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
//指定生成 WMI 连接所需的所有设置
ConnectionOptions op = new ConnectionOptions();
op.Username = txtUserName.Text.Trim(); //远程计算机用户名称
op.Password = txtPwd.Text.Trim(); //远程计算机用户密码
string str = " " + txtIP.Text.Trim() + " //root//cimv2";
//设置操作管理范围
ManagementScope scope = new ManagementScope(str, 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("Reboot", null); //执行关闭远程计算机,reboot为重新启动
}
}