Sony Arouje

a programmer's log

Archive for November 4th, 2011

Read app.config setting during installation

with 2 comments

I am working on a windows service and some specific reason the service name is stored in app.config file. One of the issue I have to solve while creating the installer is reading the service name from app.config of the service. I thought I can read it using ConfigurationManager (default approach). But it will not work out, ConfigurationManager will always return the serviceName as null, even though app.config has value. The reason behind this is the exe running my Installer is InstallUtil.exe and ConfigurationManager will look for InstallUtil.exe.config, so end up getting wrong result.

Google search shows that I have to load the config explicitly and then read the settings. To load the config file we need to specify the file path, I used reflection to get the path. The code is below

Assembly executingAssembly = Assembly.GetAssembly(typeof(WinServiceInstaller));
string targetDir = executingAssembly.Location;

Configuration config = ConfigurationManager.OpenExeConfiguration(targetDir);
string serviceName = config.AppSettings.Settings["ServiceName"].Value.ToString();


I called the above code in the constructor of WinServiceInstaller class. Now I am able to access the serviceName from the installer class.




Written by Sony Arouje

November 4, 2011 at 7:06 pm

Posted in .NET, Misc

Tagged with , ,

%d bloggers like this: