首页 > 代码库 > windows服务和桌面交互(转载)

windows服务和桌面交互(转载)

windows 服务默认位于windows 7 的session 0 中,桌面程序位于session 1中,默认情况下二者无法交互

使用如下方法可以使在服务安装完成之后默认勾选“属性”->“登录”->“允许服务与桌面交互。

 1         private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e) 2         { 3  4             SetServiceDesktopInsteract("你的服务名称"); 5             System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController(); 6             sc.ServiceName = "你的服务名称"; 7             sc.Start(); 8         } 9 10         private void SetServiceDesktopInsteract(string serviceName)11         {12             System.Management.ManagementObject wmiService = new System.Management.ManagementObject(string.Format("Win32_Service.Name=‘{0}‘", serviceName));13             System.Management.ManagementBaseObject changeMethod = wmiService.GetMethodParameters("Change");14             changeMethod["DesktopInteract"] = true;15             System.Management.ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", changeMethod, null);16         }
查看代码

仅作备忘