保存并在运行时加载的app.config(的applicationSettings) [英] Save and reload app.config(applicationSettings) at runtime

查看:208
本文介绍了保存并在运行时加载的app.config(的applicationSettings)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经存储的配置,由Visual Studio我已经创建的项目属性对话框的设置选项卡上的某个应用程序键,然后我在用户设定在应用程序级这个钥匙(未水平)。

I've stored configuration of my application in the app.config, by Visual Studio I've created some application key on the settings tab of project properties dialog, then I've set this key at application level(NOT at user level).

Visual Studio中自动生成下面的XML文件(的app.config):

Visual Studio automatically generate the following xml file (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>

现在我想保存并重新加载在运行时的设置,这是我的code,使保存配置文件中的值 DatabasePath

Now I want to save and reload the settings at runtime, here's my code that allow to save the value DatabasePath in the configuration file:

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();

问题是,这个code中的新设置不会被应用程序加载,直到我重新启动应用;有没有办法在运行时加载的配置设置?

The problem is that with this code the new settings aren't loaded by application until I restart the application; is there a way to reload the config settings at runtime?

我也想替换的applicationSettings 部分的名称(AleTest.Properties.Settings)与变量值的固定值,存在于框架变量的假设该值(AleTest.Properties.Settings)?

I also want to replace the fixed value of the name of applicationSettings section (AleTest.Properties.Settings) with a variable value, exist a variable in the framework the assume this value (AleTest.Properties.Settings) ?

推荐答案

您需要拨打电话到<一个href=\"http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.refreshsection.aspx\">ConfigurationManager.RefreshSection为了有值从磁盘重读

You need to make a call to ConfigurationManager.RefreshSection in order to have the values re-read from disk.

这篇关于保存并在运行时加载的app.config(的applicationSettings)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆