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

发布于 2024-11-15 06:33:31 字数 2280 浏览 1 评论 0原文

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

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.Settings) ?

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 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>

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

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?

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) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦幻的味道 2024-11-22 06:33:31

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

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

无所谓啦 2024-11-22 06:33:31

我做了一些测试,这是结果。

对于自动生成的类 Settings,调用 ConfigurationManager.RefreshSection("applicationSettings"); 不会对用 ApplicationScopedSettingAttribute 标记的成员应用修改后的值,它通过 ConfigurationManager 成员将更改应用于将来的调用(并且不确定 UserScopedSettingAttribute)。

而是调用 Settings.Default.Reload();

I did a some tests and here is result.

For auto generated class Settings the call of ConfigurationManager.RefreshSection("applicationSettings"); doesn't apply the modified values for members marked with ApplicationScopedSettingAttribute, it applies the changes to future calls via ConfigurationManager members (and not sure about UserScopedSettingAttribute).

Instead call Settings.Default.Reload();

烂人 2024-11-22 06:33:31

您想要的可以通过创建自定义 ConfigSection 来完成,它允许您更多控制并允许您更改名称。配置管理器有一个刷新部分,允许您重新加载数据。

What you want is accomplish able by creating an custom ConfigSection which allows you more control and allows you to change the name. Configuration manager has a refresh section which will allow you reload the data.

记忆で 2024-11-22 06:33:31

Aleroot 用于更新值的代码运行良好。
尽管 Properties.Settings 是只写的(没有设置)。

更新一下,这对我有用:
ConfigurationManager.RefreshSection(“applicationSettings”);

但我用它来访问参数:
字符串测试 = Properties.Settings.Default.MyString;
MessageBox.Show("Paramètres/Settings MyString = " + test);

Aleroot's code for updating values worked fine.
In spite of Properties.Settings being write only (no set).

To refresh, this worked for me:
ConfigurationManager.RefreshSection("applicationSettings");

But I'm using this to access the parameter:
string test = Properties.Settings.Default.MyString;
MessageBox.Show("Paramètres/Settings MyString = " + test);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文