C# 利用WMI重启进程与重启操作系统

 1  /// <summary>
2 /// 重启指定进程
3 /// </summary>
4 private void RestartProcess()
5 {
6 if (AppProperty.ProcessSet.Count > 0)
7 {
8 string processPath = "";
9 string processName = (AppProperty.ProcessSet[0] as string[])[0];
10 System.Management.ManagementObjectSearcher obj = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_Process WHERE NAME=\"EXCEL.exe\"");
11 foreach (System.Management.ManagementObject _proc in obj.Get())
12 {
13 _proc.InvokeMethod(new System.Management.ManagementOperationObserver(), "Terminate", null);
14 processPath = _proc["ExecutablePath"].ToString();
15 }
16 if (!string.IsNullOrEmpty(processPath))
17 {
18 //建立进程操作对象
19 System.Management.ManagementClass processClass = new System.Management.ManagementClass(new System.Management.ManagementPath("Win32_Process"), null);
20 //获得用来提供参数的对象
21 System.Management.ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
22 //设定命令行参数
23 inParams["CommandLine"] = processPath;
24 //执行程序
25 System.Management.ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
26 }
27 }
28 }
29
30 /// <summary>
31 /// 重启操作系统
32 /// </summary>
33 private void RestartOpSystem()
34 {
35 System.Management.ManagementObjectSearcher obj = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
36 obj.Scope.Options.EnablePrivileges = true;//连接操作启用用户特权
37 foreach (System.Management.ManagementObject opSys in obj.Get())
38 {
39 opSys.InvokeMethod("Reboot", null, null);
40 }
41 }

转载于:https://www.cnblogs.com/communism/archive/2012/02/08/memo20120208.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值