我有一个下一个App.config文件:
<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>
然后我在我的应用程序设置中对运行时进行了一些更改(我已更改了连接字符串和保存应用程序报告的路径),为了应用它们,我使用了以下代码:
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");
}
乍一看,一切运作良好 - 我在应用程序运行时看到了更改但由于某种原因,应用程序无法保存&#34; reportsFolderSettings&#34;重新启动后,我只看到默认值&#34; C:\&#34;。
P.S我从。&#34; \ bin \ release&#34;启动.exe中的应用程序文件夹,不是来自Visual Studio。
答案 0 :(得分:0)
好的,我重新创建了你的问题,我遇到了同样的问题; )
问题是
DictionarySectionHandler
仅支持读取值
这不是你问题的直接答案,但它有效
工作我这样做:
我改变了部分的类型:
<section name="reportsFolderSettings" type="System.Configuration.DictionarySectionHandler" />
使用AppSettingsSection
<section name="reportsFolderSettings" type="System.Configuration.AppSettingsSection" />
然后我改变了:
var sectionReportsFolder = config.SectionGroups["UserSettingsGroup"].Sections["reportsFolderSettings"] as AppSettingsSection;
sectionReportsFolder.Settings.Clear();
sectionReportsFolder.Settings.Add("ReportsFolder", tbo_reportfolder.Text);
然后一切都很好。我测试了这个。