Last active
June 5, 2017 15:41
-
-
Save nkundu/8d5cdfc1f94e0489febc3cb7141b0b38 to your computer and use it in GitHub Desktop.
Windows Service Installer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Install: C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe xxx.exe | |
// Uninstall: C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /u xxx.exe | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Configuration; | |
using System.Configuration.Install; | |
using System.Linq; | |
using System.Reflection; | |
using System.ServiceProcess; | |
namespace xxx | |
{ | |
[RunInstaller(true)] | |
public partial class ProjectInstaller : System.Configuration.Install.Installer | |
{ | |
public ProjectInstaller() | |
{ | |
InitializeComponent(); | |
this.Installers.Clear(); | |
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller(); | |
serviceProcessInstaller.Password = null; | |
serviceProcessInstaller.Username = null; | |
serviceProcessInstaller.Account = ServiceAccount.LocalSystem; | |
// serviceInstaller | |
ServiceInstaller serviceInstaller = new ServiceInstaller(); | |
serviceInstaller.ServiceName = GetServiceNameAppConfig("SERVICE_NAME"); | |
serviceInstaller.DisplayName = GetServiceNameAppConfig("SERVICE_DISPLAY_NAME"); | |
serviceInstaller.StartType = ServiceStartMode.Automatic; | |
serviceInstaller.Description = GetServiceNameAppConfig("SERVICE_DESCRIPTION"); | |
// kill the default event log installer | |
serviceInstaller.Installers.Clear(); | |
// add all installers | |
this.Installers.AddRange(new Installer[] { | |
serviceProcessInstaller, serviceInstaller | |
}); | |
} | |
public string GetServiceNameAppConfig(string serviceName) | |
{ | |
var config = ConfigurationManager.OpenExeConfiguration(Assembly.GetAssembly(typeof(ProjectInstaller)).Location); | |
return config.AppSettings.Settings[serviceName].Value; | |
} | |
private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e) | |
{ | |
} | |
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment