gpt4 book ai didi

c# - 在运行时保存并重新加载 app.config(applicationSettings)

转载 作者:太空狗 更新时间:2023-10-29 17:43:47 26 4
gpt4 key购买 nike

我在 app.config 中存储了我的应用程序配置,通过 Visual Studio 我在项目属性对话框的设置选项卡上创建了一些应用程序 key ,然后我在应用程序级别设置了这个 key (不是在用户等级)。

Visual Studio 自动生成以下 xml 文件 (app.config):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="AleTest.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<AleTest.Properties.Settings>
<setting name="DatabasePath" serializeAs="String">
<value>Test.s3db</value>
</setting>
<setting name="DatabaseUser" serializeAs="String">
<value />
</setting>
<setting name="DatabasePass" serializeAs="String">
<value />
</setting>
</AleTest.Properties.Settings>
</applicationSettings>
</configuration>

现在我想在运行时保存并重新加载设置,这是我的代码,它允许在配置文件中保存值 DatabasePath:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

ConfigurationSectionGroup applicationSectionGroup = config.GetSectionGroup("applicationSettings");
ConfigurationSection applicationConfigSection = applicationSectionGroup.Sections["AleTest.Properties.Settings"];
ClientSettingsSection clientSection = (ClientSettingsSection)applicationConfigSection;

//Database Configuration Setting
SettingElement applicationSetting = clientSection.Settings.Get("DatabasePath");
applicationSetting.Value.ValueXml.InnerXml = this.textBoxPath.Text.Trim();

applicationConfigSection.SectionInformation.ForceSave = true;
config.Save();

问题是,使用此代码,在我重新启动应用程序之前,应用程序不会加载新设置;有没有办法在运行时重新加载配置设置?

我还想用一个变量值替换 applicationSettings 部分 (AleTest.Properties.Settings) 名称的固定值,在框架中存在一个假设这个值 (AleTest.Properties) 的变量.设置)?

最佳答案

您需要调用ConfigurationManager.RefreshSection以便从磁盘重新读取值。

关于c# - 在运行时保存并重新加载 app.config(applicationSettings),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6335931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com