How to Install Windows Service Programmatically
Sometimes you may want to install a Windows Service programmatically, but the target machine does not have InstallUtil.exe.
To install a Windows Service programmatically, you can build an application to install that Windows Service.
- Add a reference to System.Configuration.Install
- Use this code:
public static void InstallService(string ExeFilename)
{
System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller(ExeFilename);
Installer.UseNewContext = true;
Installer.Install(null);
Installer.Commit(null);
}
To uninstall:
public static void UninstallService(string ExeFilename)
{
System.Configuration.Install.AssemblyInstaller Installer = new System.Configuration.Install.AssemblyInstaller(ExeFilename);
Installer.UseNewContext = true;
Installer.Uninstall(null);
}