Modify value in app.config

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

Alberto

I need to modify a value in the app.config file with this code:

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

string key = "Path";
string value = "New value";
config.AppSettings.Settings.Add(key, value);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");


It doesn't write the file. Could you tell me how to do it?
Thank you
 
This config is readonly.
But you can use System.Xml namespace to write smth in it
I need to modify a value in the app.config file with this code:

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

string key = "Path";
string value = "New value";
config.AppSettings.Settings.Add(key, value);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Hi,

You cannot, it;s readonly. I had a link somewhere explaining the reasons
behind this. One is that the changes in it will affect all the users of the
app, not the particular user that did it.

If you need to store changing values you can use the registry or even better
create your own config file and store it in the user ApplicationData folder
: Environment.SpecialFolder.ApplicationData

See opennetcf.org library for a good implementation of a config like class.
 
Ignacio said:
If you need to store changing values you can use the registry or even better
create your own config file and store it in the user ApplicationData folder
: Environment.SpecialFolder.ApplicationData

Is it the same as Application.UserAppDataPath?
 
Looking at Roeder's Reflector, it is identical for standard apps - however,
for ClickOnce (NetworkDeployed) apps it looks at
CurrentDomain.GetData("DataDirectory"); I'm not 100% where this value comes
from, however! (it isn't one of the special cases in Locate(), so something
must be providing it explicitly - presumably "fusion" / ClickOnce).

Marc
 
I have the same situation, I have tested the IsReadOnly properties

config.ConnectionStrings.ConnectionStrings["DBConnection"].IsReadOnly

returns false so that is writable.
 
Back
Top