using System; using System.Collections; using System.ComponentModel; using System.Configuration.Install; using System.ServiceProcess; namespace AutomaticTimingWindowsService { [RunInstaller(true)] public partial class ProjectInstaller : Installer { /// /// 初始化配置文件 /// Config config = new Config(); public ProjectInstaller() { InitializeComponent(); serviceInstaller1.ServiceName = config.ServiceName; serviceInstaller1.Description = config.ServiceDescription; serviceInstaller1.DisplayName = config.DisplayName; } /// /// 设置服务安装后“允许和桌面进行交互” /// /// protected override void OnAfterInstall(IDictionary savedState) { try { base.OnAfterInstall(savedState); // 允许服务桌面交互 System.Management.ManagementObject myService = new System.Management.ManagementObject(string.Format("Win32_Service.Name='{0}'", this.serviceInstaller1.ServiceName)); System.Management.ManagementBaseObject changeMethod = myService.GetMethodParameters("Change"); changeMethod["DesktopInteract"] = true; System.Management.ManagementBaseObject OutParam = myService.InvokeMethod("Change", changeMethod, null); } catch (Exception) { } } /// /// 重写ProjectInstaller的Commit方法 /// /// public override void Commit(IDictionary savedState) { base.Commit(savedState); ServiceController sc = new ServiceController(config.ServiceName); if (sc.Status.Equals(ServiceControllerStatus.Stopped)) { sc.Start(); } } } }