Ohad's Blog

Lets talk about .net !

Mirror at:
blogs.microsoft.co.il

News

         Ohad Israeli's Facebook profile

Grab this badge here!

C# Code Snippts

Favorite Blogs

Israeli .Net Bloggers

Binding to a custom App.Config file

Sometime there is a need to bind to a configuration file other then app.exe.config for example when creating plugins for a 3rd party host application or when deploying the app to com+ wouldn’t you prefere to use your own config file other then the appname.exe.config ? (dllhost.exe.config)

Its very easy to redirect the AppSettingsReader to a diffrent config file using :

AppDomain.CurrentDomain.SetData(“APP_CONFIG_FILE”,”c:\\ohad.config”);

Any call to System.ConfigurationSettings.AppSettings will now use the new binding…

Note: You can only bind once in app domain instance to a configuration file meaning changing the binding should be done before using the app.config for the first time.

Example:

1. Save the the following config file as c:\ohad.config

<?xml version="1.0" encoding="utf-8" ?>
<
configuration>
<appSettings>
<add key="name" value="the value"/>
</appSettings>
</
configuration>

2. Create a simple windows forms application and paste the following lines:

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE","c:\\ohad.config");
string s=System.Configuration.ConfigurationSettings.AppSettings["name"];
MessageBox.Show(s);

Comments

No Comments