Tuesday, February 08, 2011

How to Access Multiple Config Files in .Net

Problem

There is an interesting problem that appears when coding in .Net. I thought I'd post my solution to this issue and invite discussion. The problem occurs when one has a mature .Net application that uses multiple configuration files (storing settings)

One example might be when one creates a dynamic linked library (dll) and a windows or web application. Trying to reference the settings in specific configuration files has been a problem in the past. Using Visual Studio 2008, I was able to resolve this issue.

Solution

I've found what seems like a good solution to this issue. I am using VS 2008 C#. My solution involves the use of distinct namespaces between the exeutable and the dll.

So for example:
(Within Project Class Files)
namespace company.dllLibrary //used within my dll project.
namespace company.service //used within my exe project.

Settings.cs (dll project):
I've found that within the Settings.cs of the distinct projects I had to add the following statement for settings to be updated properly:
(within the setter for the specific property related to the setting)

name = value;
this.Save(); //added this to ensure setting is saved when I update the property


To read information directly from within my dll or exe project I simply leverage the appropriate namespace:
var x = company.dllLibrary.Properties.Settings.Default.SettingName;

company.dllLibrary.Properties.Settings.Default.SettingName = value;

When I want to save a value I can assign to it from within either my dll or exe project:
company.dllLibrary.Properties.Settings.Default.SettingName = value; //string, DateTime, etc

Caveats
It appears that one ought to finalize all settings within the Settings Pane because the IDE may overwrite setters/getters. I see no limit on the umber of workspaces one might leverage in this way.

Naturally if I want to reference files in alternate namespaces I could just:
company.service.Properties.Settings.Default.SettingName = value;

Impportant Note - I had to review the project properties to verify that my startup method and default namespace referenced the updated namespace. I also needed to review each of the associated xml for the app.config files to ensure that the proper namespaces were referenced in the config sections.

Hope this helps!



1 comments:

Tommie Carter said...

I've realized that probably one of the better ways out of the config hell of Visual Studio (substituted for the dll hell we used to experience) is the use of a configuration table on the datastore. I've used this in the past and with LINQ 2 SQL it seems to make even more sense (given ease of access).

Welcome to my Circle

It's amazing what one can do in this world. The ability to take the trivial and turn it inside out so that we are seeing not what is or what was, but what might have been. A journey of 1000 steps begins with one.

Light a candle and step into my circle.

燃點蠟燭,並加強到我的圈子

(z) (j)(f)(l)
Loading...