I have a next App.config file:
<configuration>
<configSections>
<sectionGroup name="UserSettingsGroup">
<section name="dbConnectionString" type="System.Configuration.ConnectionStringsSection" />
<section name="reportsFolderSettings" type="System.Configuration.DictionarySectionHandler" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<UserSettingsGroup>
<dbConnectionString>
<add name="Host" providerName="System.Data.sqlclient" connectionString="localhost" />
<add name="Port" providerName="System.Data.sqlclient" connectionString="3050" />
........
........ etc.
</dbConnectionString>
<reportsFolderSettings>
<add key="ReportsFolder" value="C:\" />
</reportsFolderSettings>
</UserSettingsGroup>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.data>
<DbProviderFactories>
<remove invariant="FirebirdSql.Data.FirebirdClient" />
<add name="FirebirdClient Data Provider" invariant="FirebirdSql.Data.FirebirdClient" description=".NET Framework Data Provider for Firebird" type="FirebirdSql.Data.FirebirdClient.FirebirdClientFactory, FirebirdSql.Data.FirebirdClient" />
</DbProviderFactories>
</system.data>
And then I made some changes in runtime in my app's settings (I've changed the connections strings and the path for saving app's reports) and for applying them I used this code:
private void SaveSettings() {
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
Configuration config = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetEntryAssembly().Location);
ConnectionStringsSection connectionSettings = (ConnectionStringsSection)config.SectionGroups.Get("UserSettingsGroup").Sections.Get("dbConnectionString");
connectionSettings.ConnectionStrings["Host"].ConnectionString = txtServer.Text;
connectionSettings.ConnectionStrings["Port"].ConnectionString = txtPort.Text;
connectionSettings.ConnectionStrings["DbPath"].ConnectionString = txtDBPath.Text;
connectionSettings.ConnectionStrings["User"].ConnectionString = txtUser.Text;
connectionSettings.ConnectionStrings["Password"].ConnectionString = txtPassword.Text;
connectionSettings.ConnectionStrings["Charset"].ConnectionString = cboCharset.Text;
Hashtable sectionReportsFolder = ConfigurationManager.GetSection(config.SectionGroups.Get("UserSettingsGroup").Sections.
Get("reportsFolderSettings").SectionInformation.SectionName) as Hashtable;
sectionReportsFolder.Clear();
sectionReportsFolder.Add("ReportsFolder", tbo_reportfolder.Text);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("dbConnectionString");
ConfigurationManager.RefreshSection("reportsFolder");
}
At first glance, everything works well - I see the changes while my app is working but for some reason the app doesn't save the "reportsFolderSettings" and after relaunch I see only default value "C:\".
P.S I launch the app from .exe from "\bin\release" folder, NOT from Visual studio.