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

David Levine said:

It's a interesting mechanism but an API to set this would be better. Also, the MSDN docs state that this property value cannot be changed after the first bind - you imply that this can be changed at any time.



# January 10, 2005 7:33 AM

Paul Wilson said:

Isn't it easier to just reference the other file in the app.config file itself:

http://weblogs.asp.net/pwilson/archive/2003/04/09/5261.aspx
# January 10, 2005 7:42 AM

Ohad Israeli said:

Paul, i know you can reference an external app.config file but there are time when you dont even want such an external file.
For example if you write an addon to outlook you dont want to create outlook.exe.config file even if it only holds references to external file.

David, I've added a note to the original post to clerify that you can only set it once. If you need to change it again there is a need to create a new appdomain instance.
# January 10, 2005 7:46 AM

Avner Kashtan said:

Cool, I always read the docs to mean you can only set it for a new AppDomain you create, not for the current one - not even once.
# January 10, 2005 2:00 PM

TrackBack said:

# January 16, 2005 7:15 PM

J Stephens said:

Thank you! I was having a stange problem in my unmanaged c++, hosting C# components app. For some reason the config variable had the full app path. Not just the name of the config file. It would combine ApplicationBase with this, and create a bogus file path. It would still somehow work when running locally, but not over the network.
Your post allowed me to set it properly. I spent about 10 hours straight looking for the cause of the problem and a solution, till I found your wonderful article. Thanks again!
# January 28, 2005 12:24 AM

Oleg said:

Hi Ohad, I have probably very stupid question, Can I use App.config file in Class library project? Is it going to create mydll.dll.config file? Regards, Oleg Glauberman.
# July 17, 2006 2:57 PM

Ohad Israeli said:

Hi Oleg

Normaly you can't but using the method described here you can.

Check also the following project : http://www.codeproject.com/dotnet/dllappconfig.asp

# July 17, 2006 5:31 PM

Charlie - cmv said:

Thank very much for this tutorial it's GREAT. Can you help us also in the scenario of what if I have(necessary) o read or get some data in both App.Config and ohad.config?

How are we going to do that?

Thank you very much. More power

# March 6, 2007 7:07 AM

tirtik said:

Hello,

this did not work for me ..

- I can set current domain's "APP_CONFIG_FILE" property.

- I check it with GetData("APP_CONFIG_FILE") function just after setting it. And I see the assignment was successful..

- However the AppSettings are not read loaded.. I get null exceptions for the settings i look for in ConfigurationManager.Appsettings[] array.

Do I need an extra call?

# July 3, 2007 7:45 AM

Ken said:

Very helpful, thank you. It's hard to find the right keywords to Google to this info. I had to wade through tons of stuff about loading a new AppDomain to accomplish the same thing that you're doing here (much more gracefully).

# November 15, 2007 10:49 PM

Nitya said:

Really helpful blog. Thanks.

# August 11, 2009 8:46 AM

Binu said:

How can i set this in WPF application using C++/CLI?

# February 3, 2010 7:19 AM

dhaval said:

Hi,

In dotnet 4.0,I need to resolve Mixed Mode assembly issue using below text in configuration file.

<?xml version="1.0"?>

<configuration>

<startup useLegacyV2RuntimeActivationPolicy="true">

<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>

</startup>

</configuration>

But, it seems to be work only in APP.CONFIG file. and does not work in custom configurationfile (custom.config).

I have tried this article but it does not recognize these elements and throw "Mixed mode assembly...." error.

Please let me know if any one has idea on this.

# September 5, 2010 12:47 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)