您现在的位置是:网站首页> 编程资料编程资料
windows中允许服务与桌面交互的更改方法示例_实用技巧_
2023-05-24
345人已围观
简介 windows中允许服务与桌面交互的更改方法示例_实用技巧_
在Windows服务的安装类ProjectInstaller中加入以下方法:
复制代码 代码如下:
protected override void OnCommitted(System.Collections.IDictionary savedState)
{
base.OnCommitted(savedState);
//将服务更改为允许桌面交互模式
ConnectionOptions coOptions = new ConnectionOptions();
coOptions.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope mgmtScope = new System.Management.ManagementScope(@"root\CIMV2", coOptions);
mgmtScope.Connect();
ManagementObject wmiService;
wmiService = new ManagementObject("Win32_Service.Name='这里是当前服务名'");
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"] = true;
ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
}
