发布:2020/4/14 10:58:01作者:管理员 来源:本站 浏览次数:997
本文实例讲述了C#实现远程关闭计算机或重启计算机的方法。分享给大家供大家参考。具体如下:
/// <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#程序设计有所帮助。
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4