Modify value in app.config

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
 
G

Guest

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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

Sericinus hunter

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?
 
M

Marc Gravell

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
 
A

Alan T

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

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

returns false so that is writable.
 

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