tags:

views:

60

answers:

1

Hi to all, I have a Windows (not Web) NET 1.1 app which can accept the settings file as its argument; this way I can easily switch between (e.g.) different connection strings just by passing a different settings file.

I understand that in NET 2.0 settings manager has been made simpler: we can define it at design-time and easily access its properties at design/run-time; but this imposes me to always use THAT particular configuration file, with no chance (as far as I understood) to have the flexibility to pass one or another configuration file accordingly to the circumstancies.

I would like to be able to have a Settings class which initializes itself just the way it does it now (app.config file in the default dir), but can be redirected to another file for read/write operations, like:

My.Settings.Use(*FileNameWithFullPathAndExtensions*)

So my question is: is there a way to do something like this?

Thanks in advance to anyone will help

+1  A: 

You can use AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\another.app.config"); to import another.app.config file as if it were this application's app.config file. It works also with typical web.config files, in case you're wondering.

After that, you can use System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString as if the string was coming from the default app.config file.

If you need to change config file after you already read the default one, you might need to refresh the sections you want to read from the new config file using ConfigurationManager.RefreshSection(), here are a few examples.

Joaquim Rendeiro
First of all, thank you very much for your reply. Then, IF I correctly understand your reply, I can use this method to read configuration from a different file, but not to write to, right?
Turro
If you need to modify it, look into ConfigurationManager.OpenExeConfiguration(), which returns a Configuration object that can be modified and saved.http://msdn.microsoft.com/en-us/library/ms224437.aspx
Joaquim Rendeiro
After having played a bit with the configurationmanager, it seems that you cannot do what I'm looking for: the second file is used to extend, not to overwrite the first one. It's too bad that the class doesn't have a FileName property... Anyway, thank you for your answers
Turro