ProjectInstaller.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Configuration.Install;
  6. using System.Linq;
  7. namespace LYFZ.WcfSystemService
  8. {
  9. [RunInstaller(true)]
  10. public partial class ProjectInstaller : System.Configuration.Install.Installer
  11. {
  12. public ProjectInstaller()
  13. {
  14. InitializeComponent();
  15. }
  16. private void ErpServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
  17. {
  18. System.ServiceProcess.ServiceController s = new System.ServiceProcess.ServiceController( LYFZ.WindowsServiceHandling.ServiceManagement.GetServiceName(LYFZ.WindowsServiceHandling.ServiceType.LYFZ_WcfSystmeService));
  19. SetServiceDesktopInsteract(s.ServiceName);
  20. if(s.Status==System.ServiceProcess.ServiceControllerStatus.Stopped){
  21. s.Start();//设置服务安装后立即启动
  22. }
  23. }
  24. #region 设置服务与桌面交互
  25. /// <summary>
  26. /// 让服务启动某个应用程序,就要修改服务的属性,勾选允许服务与桌面交互,
  27. /// 设置服务与桌面交互,在ErpServiceInstaller的AfterInstall事件中使用
  28. /// </summary>
  29. /// <param name="serviceName">服务名称</param>
  30. private void SetServiceDesktopInsteract(string serviceName)
  31. {
  32. System.Management.ManagementObject wmiService = new System.Management.ManagementObject(string.Format("Win32_Service.Name='{0}'", serviceName));
  33. System.Management.ManagementBaseObject changeMethod = wmiService.GetMethodParameters("Change");
  34. changeMethod["DesktopInteract"] = true;
  35. System.Management.ManagementBaseObject utParam = wmiService.InvokeMethod("Change", changeMethod,null);
  36. }
  37. #endregion
  38. }
  39. }