用C# 写了个Windows服务程序,在服务运行中打开了另一个需要与桌面交互的进程。可以从控制面板中服务【属性】中【登陆】页面设置 “允许服务与桌面交互”完成。可是怎么用代码方式完成选择“允许服务与桌面交互”选项。求助又实际经验的高手帮忙?

解决方案 »

  1.   

    我查了一下,问题可以解决. 请参考下列站点!
    http://www.codeproject.com/csharp/CsWindowsServiceDesktop.asp
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/changeserviceconfig.asphttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/changing_a_service_configuration.asp
      

  2.   

    在ProjectInstaller中添加一个事件ServiceInstaller_AfterInstall,然后加入下述代码,即可实现交互.参考代码:using System.Management;   //注意要添加该引用
    using System.ServiceProcess;private void ServiceInstaller_AfterInstall(object sender, System.Configuration.Install.InstallEventArgs e)
    {
    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='" + ServiceController.ServiceName + "'");ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");InParam["DesktopInteract"] = true;ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);ServiceController.Start();
    }