View Code
string key = @"SYSTEM\CurrentControlSet\Services\" + ServiceName;
注意这里的斜杠,网上大多数的例子都是/,使用这个斜杠找不到sub的值。
1 private void SetServiceTable(string ServiceName)
2 {
3 RegistryKey rk = Registry.LocalMachine;
4 string key = @"SYSTEM\CurrentControlSet\Services\" + ServiceName;
5 RegistryKey sub = rk.OpenSubKey(key, true);
6 int value = (int)sub.GetValue("Type");
7
8 //判断是否设置了服务桌面交互
9 if(value<256)
10 sub.SetValue("Type", value | 256);
11 }
12
13
14 //设置允许桌面与服务交互,不需要重启
15 private void SetServiceDesktopInsteract(string serviceName)
16 {
17
18 ManagementObject wmiService = new ManagementObject(string.Format("Win32_Service.Name='{0}'", serviceName));
19
20 ManagementBaseObject changeMethod = wmiService.GetMethodParameters("Change");
21
22 changeMethod["DesktopInteract"] = true;
23
24 ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", changeMethod, null);
25
26 }
2 {
3 RegistryKey rk = Registry.LocalMachine;
4 string key = @"SYSTEM\CurrentControlSet\Services\" + ServiceName;
5 RegistryKey sub = rk.OpenSubKey(key, true);
6 int value = (int)sub.GetValue("Type");
7
8 //判断是否设置了服务桌面交互
9 if(value<256)
10 sub.SetValue("Type", value | 256);
11 }
12
13
14 //设置允许桌面与服务交互,不需要重启
15 private void SetServiceDesktopInsteract(string serviceName)
16 {
17
18 ManagementObject wmiService = new ManagementObject(string.Format("Win32_Service.Name='{0}'", serviceName));
19
20 ManagementBaseObject changeMethod = wmiService.GetMethodParameters("Change");
21
22 changeMethod["DesktopInteract"] = true;
23
24 ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", changeMethod, null);
25
26 }