Unable to write settings to app.config (or no changes are displayed)

I am creating an application for .NET Framework 4.5

whenever I want to download application configuration values ​​(appSettings section) and record changes, I don’t see changes if I update this section. What am I doing wrong? and how can i solve it?

code

private void LoadConfig() { NameValueCollection appSettings = ConfigurationManager.AppSettings; for (int i = 0; i < appSettings.Count; i++) { switch (appSettings.GetKey(i)) { case "initialCatalog": txtInitialCatalog.Text = appSettings.GetValues(i)[0]; break; case "dataSource": txtDatasource.Text = appSettings.GetValues(i)[0]; break; case "userName": txtUsername.Text = appSettings.GetValues(i)[0]; break; case "password": txtPassword.Text = appSettings.GetValues(i)[0]; break; case "portalUrl": txtUrl.Text = appSettings.GetValues(i)[0]; break; } } } private void SaveConfig() { System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); KeyValueConfigurationCollection appSettings = config.AppSettings.Settings; appSettings["initialCatalog"].Value = txtInitialCatalog.Text; appSettings["dataSource"].Value = txtDatasource.Text; appSettings["userName"].Value = txtUsername.Text; appSettings["password"].Value = txtPassword.Text; appSettings["portalUrl"].Value = txtUrl.Text; config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); } 

App.config File

 <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <appSettings> <add key="initialCatalog" value="a" /> <add key="dataSource" value="b" /> <add key="password" value="c" /> <add key="userName" value="d" /> <add key="portalUrl" value="e" /> <add key="poolingTime" value="60000" /> <add key="ClientSettingsProvider.ServiceUri" value="" /> </appSettings> 

I call ReadConfig () when the forms are activated and save data when the button is clicked (Ok or Apply). I will close application i, run it, but no changes have been made to the app.config file.

any ideas

+4
source share
2 answers

I think that what you are testing in Visual Studio is happening, it copies App.Config to your Debug / Release directory and renames it to your YourApplication.vshost.exe.config file, which gets reset every time the application starts. Try to run the executable a file outside of Visual Studio that will use the YourApplication.exe.config file and see if this works for you. Your code works for me and saves your changes when you restart the application if I run it outside of Visual Studio.

+8
source

usually i do this:

  ... config.AppSettings.Settings.Remove("TextBoxNumber"); config.AppSettings.Settings.Add("TextBoxNumber", ""); 

It works great. This is probably because one of your keys is not yet in app.config. You need to remove / add it (for example, the above code) or create a key in app.config.

====================== Update

Try the following:

  config.AppSettings.Settings["initialCatalog"].Value = txtInitialCatalog.Text; 

instead

  appSettings["initialCatalog"].Value = txtInitialCatalog.Text; 

Any difference?

+1
source

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


All Articles