C # applicationSettings: how to update app.config?

I am using .NET 4.0, and I would like to use the app.config file to store the same parameters. I do the following. I use the Settings tab in the project properties to create my settings.

This will add the information to the app.config file as follows:

 <MyApp.Properties.Settings> <setting name="param1" serializeAs="String"> <value>True</value> </setting> <MyApp.Properties.Settings> 

In my view model (in my code) I can access the information as follows:

 bool myBool = MyApp.Properties.Default.param1; 

When I try to change the value in the configuration file, I try this:

 Properties.Settings.Default.param1 = false; 

But this leads to an error that param1 is read-only.

So how can I update my configuration file from my code?

+4
source share
4 answers

Well, I read the Hari Gillal link in which one user suggested editing the app.config file directly, that is, the XML file.

Therefore, in the project properties → settings, I create the parameters I need. Then, to load the parameter into the code, I do the following:

 _myViewModelProperty = MyApp.Properties.Settings.Default.MyParam1; 

This way, I can easily read the configuration parameter information. It is typed, so during debugging I see if this name is correct or not.

To update the configuration file, I am editing the app.config file using the .NET XML libraries.

  System.Xml.XmlDocument xml = new System.Xml.XmlDocument(); xml.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); System.Xml.XmlNode node; node = xml.SelectSingleNode("configuration/applicationSettings/MyApp.Properties.Settings/setting[@name='myparam1']"); node.ChildNodes[0].InnerText = myNewValue; xml.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); 

Thus, I create an XML document (xml variable) and load the information from the app.config file. Then I look for the node that I want to update, update its information (InnerText property), and finally save the changes.

That way I can update app.config. This is what I want, because in the portable application only one user will use it, and I want this configuration to be applied on any computer on which I run the application.

+3
source

Here is my function to update or add an entry in app.config for the "applicationSettings" section. Maybe the best way, but it works for me. If someone can suggest a better method, please share it, we will always look for something better.

  static string APPNODE = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".Properties.Settings"; static DateTime now = DateTime.Now; Utilities.UpdateConfig(APPNODE, "lastQueryTime", now.ToString()); static public void UpdateConfig(string section, string key, string value) { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ClientSettingsSection applicationSettingsSection = (ClientSettingsSection)config.SectionGroups["applicationSettings"].Sections[section]; SettingElement element = applicationSettingsSection.Settings.Get(key); if (null != element) { applicationSettingsSection.Settings.Remove(element); element.Value.ValueXml.InnerXml = value; applicationSettingsSection.Settings.Add(element); } else { element = new SettingElement(key, SettingsSerializeAs.String); element.Value = new SettingValueElement(); System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); element.Value.ValueXml = doc.CreateElement("value"); element.Value.ValueXml.InnerXml = value; applicationSettingsSection.Settings.Add(element); } config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("applicationSettings"); } 
+9
source

Mark your settings as user settings.

Detailed article: http://www.codeproject.com/Articles/25829/User-Settings-Applied

+2
source

You must use Properties.Settings to complete this action. You can see this documentation for more information.

 //modify Properties.Settings.Default.param1 = false; //save the setting Properties.Settings.Default.Save(); 

EDIT after discussion in the comments and subsequent searches:

My suggestion to achieve the desired result would be to switch to AppSettings. This is due to the fact that after some searches, I found out that appsettings changes the .config file in the Application Data folder (some tests on my machine confirm this).

Have a look at the comment in on this question .

I'm not sure if there is any work, but if you want a portable app.config file in your application, I think the only way is to switch to AppSettings, which, I am sure, can save changes to the .config application found in the program folder.

EDIT 2: Possible Solution

I found out about a possible solution to make your application portable! You can change the Provider used by the settings to save the application settings that create the custom provider.

The answer to this question is a link to the code to make applications portable. I think you will try it

+2
source

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


All Articles