467,211 Members | 1,218 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,211 developers. It's quick & easy.

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

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
Nov 16 '05 #1
  • viewed: 6710
Share:
3 Replies
APG
Hi,

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

HTH,
APG

Danny wrote:
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

Nov 16 '05 #2
Danny,

Check this article: "Application Configuration Files Explained" at
http://msdn.microsoft.com/library/de...et04222003.asp.

Alek

"Danny" <da**********@defence.gov.au> wrote in message
news:db**************************@posting.google.c om...
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

Nov 16 '05 #3
>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

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Programmer | last post: by
6 posts views Thread by Ajay Pal Singh | last post: by
1 post views Thread by Iwan Petrow | last post: by
13 posts views Thread by Alison Givens | last post: by
6 posts views Thread by robin9876@hotmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.