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
source share