1

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
whizzzkey
  • 926
  • 3
  • 21
  • 52
  • 3
    Just to inform you . When you lunch your program with visual studio the config file will be saved in YourApp.vhost.exe.config and in 'debug' or 'release' folder of you project ( depending of what you chose) . when you run .exe file directly the configuration file is YourApp.exe.config – nAviD Feb 22 '16 at 11:51

1 Answers1

0

OK, I recreated Your problem and I had the same problem ; )

The problem is DictionarySectionHandler supports only reading values

This is not direct answer to Your question but it works

To work I do this:

I change type of section from:

 <section name="reportsFolderSettings" type="System.Configuration.DictionarySectionHandler" /> 

to use the AppSettingsSection

 <section name="reportsFolderSettings" type="System.Configuration.AppSettingsSection" />

Then I change:

        var sectionReportsFolder = config.SectionGroups["UserSettingsGroup"].Sections["reportsFolderSettings"] as AppSettingsSection;

        sectionReportsFolder.Settings.Clear();

        sectionReportsFolder.Settings.Add("ReportsFolder", tbo_reportfolder.Text);

And then everything is work good. I tested this.

blogprogramisty.net
  • 1,714
  • 1
  • 18
  • 22
  • Great thx! I've spend more than 3 hours with this problem, obviously the problem is DictionarySectionHandler which supports only read values – whizzzkey Feb 22 '16 at 13:41