Is it possible to read runtime changes in App.config?

  • Thread starter Thread starter Danny
  • Start date Start date
D

Danny

Hello,

I was wondering if anyone knew if it was possible to make changes to
an application's App.config file at runtime and then be able to access
those changes immediately.

The following is an example of what I'm trying to achieve:

I tried a test by creating a simple app Test.exe that has some basic
settings made available to it in the runtime file Test.exe.config.
E.g.

<appSettings>
<add key="setting" value="123" />
</appSettings>

I then started the app, and printed out the setting through a call to
ConfigurationSettings.AppSettings["setting"] with result:

setting = 123

Then, while the app was still running, I changed the value of the
setting as such, and saved the file Test.exe.config:

<appSettings>
<add key="setting" value="abc" />
</appSettings>

Again I printed the out the setting through the call to
ConfigurationSettings.AppSettings["setting"] but there was no change,
the output was still

setting = 123

whereas I would expect it to be

setting = abc

It seems that the contents of the App.config file are loaded into some
cache upon startup, and any physical changes to the file itself go
unnoticed until the application is instantiated again.

Is this the case? If so, any ideas on how to go about achieving what I
am trying to do. I figure the only option would be to create a custom
configuration class that reads from some custom config file when
requested.

Hope to hear from someone,

Danny
 
Danny said:
Hello,

I was wondering if anyone knew if it was possible to make changes to
an application's App.config file at runtime and then be able to access
those changes immediately.

The System.Configuration classes seem to cache the app.config settings
for performance reasons I suppose.
Is this the case? If so, any ideas on how to go about achieving what I
am trying to do. I figure the only option would be to create a custom
configuration class that reads from some custom config file when
requested.

Yes, that's what I would do. But instead of reading your custom config
file whenever requested you could use a FilesystemWatcher (see System.IO
namespace) to detect changes and reload the file.

Martin
 
Hi,

Probabily you can use the FileSystemWatcher to create notifications when
there are changes to this file.

HTH,
APG
 
I was wondering if anyone knew if it was possible to make changes to
an application's App.config file at runtime and then be able to access
those changes immediately.

Not without a lot of code of your own, for two main reasons:

1) The .NET configuration system will read its settings at app startup
only, and not ever after that - so any changes you may have made will
never be reflected in the ConfigurationSettings object

2) The .NET config system does not offer any way to manipulate an
existing .config file out of the box. And this is by design - under
normal circumstances, your app's .config file will reside in the app's
directory, where regular users should *not* have any write permissions
in the first place - so you wouldn't be able to modify that file
anyway.

Instead, for user settings, I'd recommend using some kind of a
XML-based config file, and store it e.g. using the IsolatedStorage
classes, to ensure the user can actually write back changes to his
settings!

Also, there are a ton of free third-party configuration classes and
components - just go search www.codeproject.com for "configuration
..NET" or something like that - you'll find quite a few links.

Marc
 
Back
Top