app.config — 如何在运行时强制从文件加载? [英] app.config — How to force load from file at runtime?

查看:24
本文介绍了app.config — 如何在运行时强制从文件加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个命令行应用程序并将它的很多配置移到一个标准的设置文件中.所有设置都声明为 Scope = Application,因为应用程序中的逻辑没有任何特定于用户的内容.我使用

I created a command-line application and moved a lot of its config into a standard Settings file. All settings are declared as Scope = Application, because there is nothing user-specific about the logic in the application. I access the values throughout the code with

Properties.Settings.Default.<whatever>  

这很有效,因为它会按计划自动运行.配置文件中的更新值反映在输出中.
一段时间后,我创建了一个基本的 GUI(在同一个命名空间中)来直接启动命令行应用程序(通过一个单独的构造函数).我没有做过大量的 .Net 编程,但我基本上像 DLL 一样使用我的 CLI 应用程序(我不知道是否有一个合适的术语;在我的输出文件夹中,我只需要 GUI.exe、CLI.exe 和 CLI.exe.config 并且它可以工作).但是,我注意到以这种方式启动时,没有加载 CLI.exe.config 文件;CLI 应用程序仅使用其编译的默认值.我希望配置文件方法可以在这种情况下工作.
我已经尝试了以下方法来强制加载配置文件,但到目前为止已经画了一个空白:

This works well as it runs automatically on a schedule. Updating values in the config file are reflected in the output.
Some time later, I created a basic GUI (in the same namespace) to launch the command-line application directly (through a separate constructor). I haven't done a huge amount of .Net programming, but I'm basically using my CLI application like a DLL (I don't know if there's a proper term for this; in my output folder, all I need is GUI.exe, CLI.exe and CLI.exe.config and it works). However, I've noticed that when launched this way, the CLI.exe.config file is not loaded; the CLI application uses only its compiled-in defaults. I was hoping the config file method would work in this instance.
I've tried the following methods to force loading the config file but so far have drawn a blank:

ConfigurationManager.RefreshSection("appSettings")  

2:

ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = Path.Combine(Environment.CurrentDirectory, System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".exe.config");
ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None).Save(ConfigurationSaveMod.Modified);  
ConfigurationManager.RefreshSection("appSettings");  

3:

Properties.Settings.Default.Reload();  

这些都不会产生错误,但是我在配置文件中修改的 Properties.Settings.Default.Value 没有更新.有没有办法在这里完成我所需要的?

None of these produce errors, but the Properties.Settings.Default.Value I have modified in the config file is not updated. Is there a way to accomplish what I need here?

这里是我的 CLI.exe.config 文件的示例,如果它有助于说明我在这里尝试完成的工作:

here is a sample of my CLI.exe.config file if it helps illustrate what I'm trying to accomplish here:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="CLI.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    </sectionGroup>
  </configSections>
  <connectionStrings/>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <applicationSettings>
    <CLI.Properties.Settings>
      <setting name="URLBase" serializeAs="String">
        <value>https://cloud.mycompany.com/</value>
      </setting>
      <setting name="URLPage" serializeAs="String">
        <value>/inventory.aspx#view/Invoice/</value> <!-- This is the value I'm trying to change -->
      </setting>
...

我还应该提到,我还在上面的代码中尝试过用applicationSettings"代替appSettings".

I should also mention that I've also tried 'applicationSettings' in place of 'appSettings' in the code above.

推荐答案

实际 config 文件,其中包含运行时设置将取决于应用程序的入口点.

Actual config file which will have your runtime settings will depend on your application's entry point.

如果您直接启动 CLI.exe,它将使用用户本地应用程序数据文件夹内的子文件夹中的用户设置文件,类似于:

If you start your CLI.exe directly, it will use a user settings file inside a subfolder inside user's local app data folder, similar to:

AppData\Local\CLI\CLI.exe\1.2.3.4\user.config

但是,如果您使用引用 GUI.exe 的不同入口点 (CLI.exe),则所有对 Properties.Settings 的调用> 将使用一个完全不同的 user.config 实例,位于(类似):

However, if you use a different entry point (CLI.exe), which references GUI.exe, all calls to Properties.Settings will use a completely different user.config instance, located in (something like):

AppData\Local\GUI\GUI.exe\1.2.3.4\user.config

应用程序范围的设置文件 (.exe.config) 位于您的输出文件夹中的可执行文件旁边,仅提供这些用户设置的默认设置,或只读应用程序设置.允许应用程序写入此文件意味着它将具有对 Program Files 子文件夹的写入权限,这在没有提升权限的情况下是不可能的.

Application-wide settings file (.exe.config) which is placed in your output folder, next to the executable, only provides default settings for these user settings, or read-only application settings. Allowing the application to write to this file would mean it would have write access to Program Files subfolders, which is not possible without elevated permissions.

在更改入口点时获得相同用户设置的最简单解决方案是将 user.config 文件从 CLI 应用程序数据文件夹复制到适当的文件夹用于新的入口点.

The simplest solution to get the same user settings when you change your entry point is to copy the user.config file from the CLI app data folder to the appropriate folder for the new entry point.

(应用设置更新)

它对应用程序设置(即输出文件夹中的 .config 文件)的工作方式相同.如果您最初拥有:

It works the same for application settings (i.e. .config files in your output folder). If you originally had:

<!-- CLI.exe.config -->
<configuration>

    <!-- setting section definitions -->
    <configSections>
        <sectionGroup name="userSettings" ... >
            <section name="CLI.CliSettings" ... />
        </sectionGroup>
        <sectionGroup name="applicationSettings" ... >
            <section name="CLI.CliSettings" ... />
        </sectionGroup>
    </configSections>

    <!-- defaults for user settings (runtime changes are stored to appdata) -->
    <userSettings>
        <CLI.CliSettings>
            <setting name="SomeUserCliSetting" serializeAs="String">
                <value>Some CLI user setting</value>
            </setting>
        </CLI.CliSettings>
    </userSettings>

    <!-- application settings (readonly) -->
    <applicationSettings>
        <CLI.CliSettings>
            <setting name="SomeAppCliSetting" serializeAs="String">
                <value>Some CLI app setting</value>
            </setting>
        </CLI.CliSettings>
    </applicationSettings>

</configuration>

而你的 GUI.exe.config 是:

And your GUI.exe.config was:

<!-- GUI.exe.config -->
<configuration>

    <configSections>
        <sectionGroup name="userSettings" ... >
            <section name="GUI.GuiSettings" ... />
        </sectionGroup>
        <sectionGroup name="applicationSettings" ... >
            <section name="GUI.GuiSettings" ... />
        </sectionGroup>
    </configSections>

    <userSettings>
        <GUI.GuiSettings>
            <setting name="SomeGuiUserSetting" serializeAs="String">
                <value>Some GUI user setting</value>
            </setting>
        </GUI.GuiSettings>
    </userSettings>

    <applicationSettings>
        <GUI.GuiSettings>
            <setting name="SomeGuiAppSetting" serializeAs="String">
                <value>Some GUI app setting</value>
            </setting>
        </GUI.GuiSettings>
    </applicationSettings>

</configuration>

那么您生成的 .config 文件应包含所有部分:

Then your resulting .config file should contain all sections:

<!-- GUI.exe.config -- merged configuration sections -->
<configuration>

    <configSections>
        <sectionGroup name="userSettings" ... >
            <section name="GUI.GuiSettings" ... />
            <section name="CLI.CliSettings" ... />
        </sectionGroup>
        <sectionGroup name="applicationSettings" ... >
            <section name="GUI.GuiSettings" ... />
            <section name="CLI.CliSettings" ... />
        </sectionGroup>
    </configSections>

    <userSettings>
        <GUI.GuiSettings>
            <setting name="SomeGuiUserSetting" serializeAs="String">
                <value>Some GUI user setting</value>
            </setting>
        </GUI.GuiSettings>
        <CLI.CliSettings>
            <setting name="SomeUserCliSetting" serializeAs="String">
                <value>Some CLI user setting</value>
            </setting>
        </CLI.CliSettings>
    </userSettings>

    <applicationSettings>
        <GUI.GuiSettings>
            <setting name="SomeGuiAppSetting" serializeAs="String">
                <value>Some gui app setting</value>
            </setting>
        </GUI.GuiSettings>
        <CLI.CliSettings>
            <setting name="SomeAppCliSetting" serializeAs="String">
                <value>Some CLI app setting</value>
            </setting>
        </CLI.CliSettings>
    </applicationSettings>

</configuration>

(可能我在写这篇文章时混淆了一些价值观,但你明白了).

(it's possible I mixed up some values while writing this, but you get the idea).

还有一个我通过谷歌搜索找到的工具:XmlConfigMerge onCodeProject - 我还没有尝试过,但大概它会自动做同样的事情,所以你可能想要检查它并将它包含到你的构建脚本中.

There is also a tool I found by googling: XmlConfigMerge on CodeProject - I haven't tried it but presumably it does the same thing automatically, so you might want to check it out and include it into your build scripts.

这篇关于app.config — 如何在运行时强制从文件加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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