的app.config - 如何在运行时从文件中力载荷? [英] app.config — How to force load from file at runtime?

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

问题描述

我创建了一个命令行应用程序,并感动了很多其配置成一个标准的设置文件。所有设置都宣布为范围=应用程序,因为没有什么特定用户有关应用程序的逻辑。我访问整个code的值,其

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>  

这工作得很好,因为它按计划自动运行。在配置文件中更新的值反映在输出。
一段时间后,我创建了一个基本的图形用户界面(在同一个命名空间),直接启动命令行应用程序(通过一个单独的构造函数)。我没有做过了大量的.Net编程,但我基本上是使用像一个DLL(我不知道是否有一个适当的期限为这个我的CLI应用程序;在我的输出文件夹,我需要的是图形用户界面。 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>
...

我还要提到的是我也试着代替在code以上的appSettings'的applicationSettings。

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

推荐答案

实际配置文件,该文件将有你的运行时设置将取决于您的应用程序的入口点。

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

不过,如果你使用一个不同的切入点( CLI.exe ),它引用 GUI.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 ),这是摆在你的输出文件夹,旁边的可执行文件,只提供默认设置为这些用户设置,或只读的应用程序设置。允许应用程序写入到该文件将意味着它将有写权限程序文件的子文件夹,这是不可能没有提升的权限。

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在$ C $的CProject - 我还没有尝试过,但presumably它会自动同样的事情,所以你可能要检查出来,包括到您的构建脚本

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天全站免登陆