windows中允許服務(wù)與桌面交互的更改方法

字號:


    這篇文章主要介紹了windows中允許服務(wù)與桌面交互的更改方法,需要的朋友可以參考下
    在Windows服務(wù)的安裝類ProjectInstaller中加入以下方法:
     代碼如下:
    protected override void OnCommitted(System.Collections.IDictionary savedState)
    {
    base.OnCommitted(savedState);
    //將服務(wù)更改為允許桌面交互模式
    ConnectionOptions coOptions = new ConnectionOptions();
    coOptions.Impersonation = ImpersonationLevel.Impersonate;
    ManagementScope mgmtScope = new System.Management.ManagementScope(@"rootCIMV2", coOptions);
    mgmtScope.Connect();
    ManagementObject wmiService;
    wmiService = new ManagementObject("Win32_Service.Name='這里是當(dāng)前服務(wù)名'");
    ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
    InParam["DesktopInteract"] = true;
    ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
    }