Working with app.config

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
 
S

scase

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
 
K

Kevin Spencer

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.
 
A

Alberto

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.
 
A

Alberto

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top