views:

594

answers:

3

I'm using C# .NET 2.0 Windows Application.

and I'm using app.config for my Application Settings.

but change in AppSettings doesn't reflected runtime, it Needs Application to be restarted.

How can I avoid it.

Here is my code snippet I used to read and write the Application Settings.

I'm reading the Setting like this

string temp = ConfigurationManager.AppSettings.Get(key);

I'm updating the value like this where node is the current configuration/appSettings Node

node.Attributes["value"].Value = value;
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
+13  A: 

You could try calling

ConfigurationManager.RefreshSection("appSettings")
to refresh the AppSettings section of the file from disk. Once they have been refreshed, you should be able to read the new values.

I've just tested this and it does indeed work.

adrianbanks
+1 Very cool - I didn't know you could do that!
Andrew Hare
RefreshSection will not work
Khalil Dahab
Its Working !!!
Tumbleweed
+1  A: 

Dont use ConfigurationManager to read settings, instead use:

        System.Configuration.ConfigurationManager.OpenExeConfiguration(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile).AppSettings.Settings["value"];
Khalil Dahab
Yes this can also possible, but it's probably not the most efficient way to do it.
Tumbleweed
here you use ConfigurationManager too.
SwissCoder
+3  A: 

Alternatively, you could create a singleton 'Options' to hold on to your application settings and perform your read/writes for you. Once loaded, changing the .config doesn't require reloading, you simply set a property on the singleton and call your .Save() method.

The 'runtime' version of your settings is in the singleton, no need to read from disk.

Marc
+1, In humild my opnion, that is the best solution till now.
Cleiton