windows中允许服务与桌面交互的更改方法示例
程序员文章站
2024-02-26 20:53:52
在windows服务的安装类projectinstaller中加入以下方法:
复制代码 代码如下:protected override void oncommitted(...
在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);
}