本文实例讲述了C#实现远程关闭计算机或重启计算机的方法。分享给大家供大家参考。具体如下:
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
/// <summary>
/// 远程关闭计算机或重启计算机
/// </summary>
/// <param name="str">命令</param>
/// <param name="ip">ip地址</param>
static void Invoke(string str, string ip)
{
//定义连接远程计算机的一些选项
ConnectionOptions options = new ConnectionOptions();
options.Username = "administrator";
options.Password = "Btmu@123";
ManagementScope scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2", options);
try
{
//用给定管理者用户名和口令连接远程的计算机
scope.Connect();
ObjectQuery oq = new ObjectQuery("select * from win32_OperatingSystem");
ManagementObjectSearcher query1 = new ManagementObjectSearcher(scope, oq);
ManagementObjectCollection queryCollection1 = query1.Get();
foreach (ManagementObject mo in queryCollection1)
{
string[] ss = { "" };
if (str == "重新启动")
{
mo.InvokeMethod("Reboot", ss);
}
if (str == "关闭计算机")
{
mo.InvokeMethod("Shutdown", ss);
}
}
}
catch (Exception er)
{
Console.WriteLine("连接" + ip + "出错,出错信息为:" + er.Message);
}
}
|
希望本文所述对大家的C#程序设计有所帮助。








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。