当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现远程关闭计算机或重启计算机的方法

C#实现远程关闭计算机或重启计算机的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网