Sometimes it is required to make the Windows service name configuration based. It means service name will fetch from App.config and install the service with the same name. In a simple way, it is not fetching the correct value from App.config at the time of installation. Because the exe running Installer is InstallUtil.exe and ConfigurationManager will look for InstallUtil.exe.config, it provides the wrong result. Follow the below steps to achieve this.

Implementation

Step 1

Add following appSetting key in App.config to have service name.

<?xmlversion="1.0"encoding="utf-8"?>

<configuration>

<startup>

<supportedRuntimeversion="v4.0"sku=".NETFramework,Version=v4.5.1"/>

</startup>

<appSettings>

<addkey="ServiceName"value="ServiceMonitorV1"/>

</appSettings>

</configuration>

Next, right click on App.config and select properties. Change below two properties values,