Sony Arouje

a programmer's log

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 , ,

2 Responses

Subscribe to comments with RSS.

  1. work´s fine

    Thanks

    Anonymous

    August 7, 2015 at 1:15 pm

  2. Great job!

    Anonymous

    September 7, 2016 at 6:55 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: