/// <summary> /// 获取安装信息 /// </summary> /// <param name="assembly"></param> /// <param name="serviceInstallerList"></param> /// <param name="serviceProcessInstallerList"></param> /// <returns></returns> private List <ServiceInstallInfo> GetInstallInfoList(Assembly assembly, List <ServiceInstaller> serviceInstallerList, List <ServiceProcessInstaller> serviceProcessInstallerList) { Int32 loopTimes = Math.Min(serviceInstallerList.Count, serviceProcessInstallerList.Count); List <ServiceInstallInfo> retVal = new List <ServiceInstallInfo>(); ServiceInstallInfo installInfo; for (Int32 i = 0; i < loopTimes; i++) { if (serviceInstallerList[i] == null || serviceProcessInstallerList[i] == null) { continue; } installInfo = new ServiceInstallInfo(); installInfo.ServiceFilePath = Path.GetFullPath(assembly.Location); installInfo.ServiceName = serviceInstallerList[i].ServiceName; installInfo.DisplayName = serviceInstallerList[i].DisplayName; installInfo.Description = serviceInstallerList[i].Description; installInfo.StartType = serviceInstallerList[i].StartType; installInfo.IfDelayedAutoStart = serviceInstallerList[i].DelayedAutoStart; installInfo.ServicesDependedOn = serviceInstallerList[i].ServicesDependedOn; installInfo.InstallLogFilePath = Path.GetFullPath(String.Format("{0}\\{1}.InstallLog" , Path.GetDirectoryName(Path.GetFullPath(assembly.Location)) , Path.GetFileName(assembly.Location))); installInfo.ServiceAccount.Account = serviceProcessInstallerList[i].Account; installInfo.ServiceAccount.UserName = String.IsNullOrEmpty(serviceProcessInstallerList[i].Username) ? String.Empty : serviceProcessInstallerList[i].Username; installInfo.ServiceAccount.Password = String.IsNullOrEmpty(serviceProcessInstallerList[i].Password) ? String.Empty : serviceProcessInstallerList[i].Password; retVal.Add(installInfo); } return(retVal); }
/// <summary> /// 服务卸载 /// </summary> /// <param name="serviceInfos"></param> public void UnInstall(ServiceInstallInfo serviceInfo) { if (serviceInfo == null || String.IsNullOrWhiteSpace(serviceInfo.ServiceName)) { throw new ArgumentNullException("serviceInfo" , TextManager.DefaultInstance.GetTextWithTail(Texts.DotNetServiceInstaller_06)); } this.UnInstall(serviceInfo.ServiceName, serviceInfo.InstallLogFilePath); }
/// <summary> /// 服务安装 /// </summary> /// <param name="installInfo"></param> public void Install(ServiceInstallInfo installInfo) { if (installInfo == null || String.IsNullOrWhiteSpace(installInfo.ServiceName) || String.IsNullOrWhiteSpace(installInfo.ServiceFilePath)) { throw new ArgumentNullException("installInfo" , TextManager.DefaultInstance.GetTextWithTail(Texts.DotNetServiceInstaller_02)); } ServiceInstallerHelper installerHelp = new ServiceInstallerHelper(); try { installerHelp.ServiceFilePath = installInfo.ServiceFilePath; installerHelp.ServiceName = installInfo.ServiceName; installerHelp.DisplayName = installInfo.DisplayName; installerHelp.Description = installInfo.Description; installerHelp.StartType = installInfo.StartType; installerHelp.IfDelayedAutoStart = installInfo.IfDelayedAutoStart; installerHelp.ServicesDependedOn = installInfo.ServicesDependedOn; installerHelp.InstallLogFilePath = installInfo.InstallLogFilePath; installerHelp.ServiceAccount = installInfo.ServiceAccount.Account; installerHelp.UserName = installInfo.ServiceAccount.UserName; installerHelp.Password = installInfo.ServiceAccount.Password; installerHelp.Install(new Hashtable()); if (installInfo.IfStartAfterInstall) { try { using (ServiceController controller = new ServiceController(installInfo.ServiceName)) { controller.Start(); controller.Close(); } } catch { } } } catch { throw; } }
public void Uninstall() { DotNetServiceInstaller installer = new DotNetServiceInstaller(); ServiceInstallInfo installInfo = new ServiceInstallInfo(); installInfo.ServiceName = ServiceName; installInfo.DisplayName = DisplayName; installInfo.Description = Description; try { installer.UnInstall(installInfo); MessageBox.Show("卸载完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(String.Format("对不起,卸载失败!\r\n原因:{0}", ex.ToString())); } }
public void Install() { DotNetServiceInstaller installer = new DotNetServiceInstaller(); ServiceInstallInfo installInfo = new ServiceInstallInfo(); // -service installInfo.ServiceFilePath = $"\"{Assembly.GetEntryAssembly().Location}\" -service"; installInfo.ServiceName = ServiceName; installInfo.DisplayName = DisplayName; installInfo.Description = Description; installInfo.StartType = ServiceStartMode.Automatic; installInfo.IfDelayedAutoStart = true; installInfo.ServiceAccount.Account = ServiceAccount.LocalSystem; try { installer.Install(installInfo); MessageBox.Show("安装完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(String.Format("对不起,安装失败!\r\n原因:{0}", ex.ToString())); } }
/// <summary> /// 获取安装信息 /// </summary> /// <param name="assembly"></param> /// <param name="serviceInstallerList"></param> /// <param name="serviceProcessInstallerList"></param> /// <returns></returns> private List<ServiceInstallInfo> GetInstallInfoList(Assembly assembly, List<ServiceInstaller> serviceInstallerList, List<ServiceProcessInstaller> serviceProcessInstallerList) { Int32 loopTimes = Math.Min(serviceInstallerList.Count, serviceProcessInstallerList.Count); List<ServiceInstallInfo> retVal = new List<ServiceInstallInfo>(); ServiceInstallInfo installInfo; for (Int32 i = 0; i < loopTimes; i++) { if (serviceInstallerList[i] == null || serviceProcessInstallerList[i] == null) continue; installInfo = new ServiceInstallInfo(); installInfo.ServiceFilePath = Path.GetFullPath(assembly.Location); installInfo.ServiceName = serviceInstallerList[i].ServiceName; installInfo.DisplayName = serviceInstallerList[i].DisplayName; installInfo.Description = serviceInstallerList[i].Description; installInfo.StartType = serviceInstallerList[i].StartType; installInfo.IfDelayedAutoStart = serviceInstallerList[i].DelayedAutoStart; installInfo.ServicesDependedOn = serviceInstallerList[i].ServicesDependedOn; installInfo.InstallLogFilePath = Path.GetFullPath(String.Format("{0}\\{1}.InstallLog" , Path.GetDirectoryName(Path.GetFullPath(assembly.Location)) , Path.GetFileName(assembly.Location))); installInfo.ServiceAccount.Account = serviceProcessInstallerList[i].Account; installInfo.ServiceAccount.UserName = String.IsNullOrEmpty(serviceProcessInstallerList[i].Username) ? String.Empty : serviceProcessInstallerList[i].Username; installInfo.ServiceAccount.Password = String.IsNullOrEmpty(serviceProcessInstallerList[i].Password) ? String.Empty : serviceProcessInstallerList[i].Password; retVal.Add(installInfo); } return retVal; }
/// <summary> /// 服务卸载 /// </summary> /// <param name="serviceInfos"></param> public void UnInstall(ServiceInstallInfo serviceInfo) { if (serviceInfo == null || String.IsNullOrWhiteSpace(serviceInfo.ServiceName)) throw new ArgumentNullException("serviceInfo" , TextManager.DefaultInstance.GetTextWithTail(Texts.DotNetServiceInstaller_06)); this.UnInstall(serviceInfo.ServiceName, serviceInfo.InstallLogFilePath); }
/// <summary> /// 服务安装 /// </summary> /// <param name="installInfo"></param> public void Install(ServiceInstallInfo installInfo) { if (installInfo == null || String.IsNullOrWhiteSpace(installInfo.ServiceName) || String.IsNullOrWhiteSpace(installInfo.ServiceFilePath)) throw new ArgumentNullException("installInfo" , TextManager.DefaultInstance.GetTextWithTail(Texts.DotNetServiceInstaller_02)); ServiceInstallerHelper installerHelp = new ServiceInstallerHelper(); try { installerHelp.ServiceFilePath = installInfo.ServiceFilePath; installerHelp.ServiceName = installInfo.ServiceName; installerHelp.DisplayName = installInfo.DisplayName; installerHelp.Description = installInfo.Description; installerHelp.StartType = installInfo.StartType; installerHelp.IfDelayedAutoStart = installInfo.IfDelayedAutoStart; installerHelp.ServicesDependedOn = installInfo.ServicesDependedOn; installerHelp.InstallLogFilePath = installInfo.InstallLogFilePath; installerHelp.ServiceAccount = installInfo.ServiceAccount.Account; installerHelp.UserName = installInfo.ServiceAccount.UserName; installerHelp.Password = installInfo.ServiceAccount.Password; installerHelp.Install(new Hashtable()); if (installInfo.IfStartAfterInstall) { try { using (ServiceController controller = new ServiceController(installInfo.ServiceName)) { controller.Start(); controller.Close(); } } catch { } } } catch { throw; } }