本文实例讲述了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#程序设计有所帮助。
如对本文有疑问, 点击进行留言回复!!
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
C#实现获取本地内网(局域网)和外网(公网)IP地址的方法分析
网友评论