Working with app.config

  • Thread starter Thread starter Alberto
  • Start date Start date
A

Alberto

I have a app.config file and I'd like to read and modify strings in it.

I can read but I can't modify strings. The code I'm using is this:

System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

string key = "myKey";
config.Save(ConfigurationSaveMode.Modified);

Configuration.Manager.RefreshSection("appSettings");

What's my mistake? Thank you very much
 
Alberto,

I am not sure if this is a bug in .NET, but we have found that we cannot
write to the app.config file either. We have found a work around though.

In your main App.Config you would point to another config that you are
allowed to make changes to.

Example: (App.Config)
<configuration>
<appSettings file="NewApp.config"></appSettings>
</configuration>

Example: (NewApp.Config)
<?xml version="1.0" encoding="utf-8"?>
<appSettings>
Put all of your app settings in here
</appSettings>
------

Now if you make changes to NewApp.Config you have to close and restart the
application in order for the new settings to take effect.

Hope this helps. If anyone has a better solution to this please let me
know.

Sincerely,
Shannon
 
From the documentation:

If the configuration file has changed since this Configuration object was
created, a run-time error occurs.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

I had the same problem once. Fixed it using the same solution.
 
so, it can't be changed???

Kevin Spencer said:
From the documentation:

If the configuration file has changed since this Configuration object was
created, a run-time error occurs.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

I had the same problem once. Fixed it using the same solution.
 
Sorry but I don't understand the way I should do this. Do you meant that
after read the current app.config with a Configuration object, I should
create another Configuration object to write the changes?
Thank you
 
Back
Top