How to change application settings at runtime

I try to change at runtime one key of my application settings file, but it does not work.

I do like this:

ConfigurationSettings.AppSettings["XPTO"] = "HELLO"; 

It seems to change only in memory, not in the file.

Does anyone know how to do this?

Thanks.

+3
source share
3 answers

Take a look at my overview of the .NET settings files ... In short, I think you want to customize your reach. He will behave as you expect.

Edit: If you use the settings designer in Visual Studio , just change the "Scope" setting to "User." If not, you should be able to do the equivalent programmatically.

+9
source

The AppSettings file is not for writing. It is designed to store configurations that will not change at run time, but may change over time, for example: database connection strings, web service URLs, etc.

So, although it is possible to update the file in reality, you should repeat if this value should be stored there.

+5
source

Assuming your application has write permissions to the file ...

 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // the config that applies to all users AppSettingsSection appSettings = config.AppSettings; if (appSettings.IsReadOnly() == false) { appSettings("Key").Value = "new value"; config.Save(); } 

I ignore all possible exceptions that can be thrown ...

+5
source

Source: https://habr.com/ru/post/1444764/


All Articles