C#: Set ServiceName using an Installation Parameter
Often it is necessary to set the ServiceName for a Windows service at the time of installation. This is helpful when there will be more than once installation of the same server or if a installation specific name is needed. One way to set the ServiceName is to pass a parameter during installation that is read by the Installer Class:
private void BeforeInstallEventHandler( object sender, InstallEventArgs e )
// Add steps to perform any actions before the install process.
string name = Context.Parameters["ServiceName"];
if ( name != null )
serviceInstaller.ServiceName = name;
serviceInstaller.ServiceName = "YourServiceName";
The InstallContext.Parameters Property is used to obtain the parameters that were used when the installutil.exe was run. In the above example (don't forget your exception handling) if the parameter "ServiceName" is used then the service is installed with the name specified. If it is not specified then the "default" servicename is used.
The installation of the service could be executed with the following: