C#远程关机代码
C#远程关机代码:
方法1
//按钮点击事件
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就是重启
}
}
方法2
options.Username = textBox2.Text; //用户名
options.Password = textBox3.Text; //用户口令
try
{
ManagementScope Conn = new ManagementScope("\\\\
Conn.Connect();
//确定WMI操作的内容
ObjectQuery oq = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher query1 = new ManagementObjectSearcher(Conn, oq);
//获取WMI操作内容
ManagementObjectCollection queryCollection1 = query1.Get();
//根据使用者选择执行相应的远程操作
foreach (ManagementObject mo in queryCollection1)
{
string [ ] ss= { "" };
if (comboBox1.Text == "重新启动")
mo.InvokeMethod("Reboot", ss); //执行重启操作
else if (comboBox1.Text == "远程关机")
mo.InvokeMethod("Shutdown", ss); //执行远程关机
else
MessageBox.Show ("选择不正确的操作!", "错误!");
}
}
catch(Exception ee)
{
MessageBox.Show("连接" + textBox1.Text + "出错,出错信息为:" + ee.Message ); //报错
}
第3种方法:
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("
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为重新启动
}
}
}
}
第4方法:
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 = ""; //远程计算机用户密码
//设置操作管理范围
ManagementScope scope = new ManagementScope("
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为重新启动
}
}
}