欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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);
}