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);

11 Comments

  • 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.







  • 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.

  • 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.

  • 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.

  • 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

  • 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

  • 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?

  • 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).

  • Really helpful blog. Thanks.

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

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








    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.


Comments have been disabled for this content.