使用 user.config 而不是 app.config

发布于 2024-10-21 00:03:01 字数 1623 浏览 5 评论 0原文

有人可以给我一个关于如何使用用户设置而不是应用程序设置的简单示例吗?我需要有特定于用户的 Microsoft Unity 部分,但在应用程序启动时不会为用户创建配置。另外,我无法使用 Visual Studio gui 来创建这些设置。我还需要在运行时修改一些映射。

这是我在用户配置中需要的(应放置在用户的应用程序数据中)

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="MyUnityContainer" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
      </configSections>
      <connectionStrings>
        <add name="MyProgram.Properties.Settings.MyConnectionString" connectionString="aConnectionString" />
      </connectionStrings>
      <MyUnityContainer>
        <typeAliases>
          <!-- type aliases -->
        </typeAliases>
        <containers>
          <container>
            <types>
              <!-- types -->
            </types>
            <extensions>
              <add type="Microsoft.Practices.Unity.InterceptionExtension.Interception, Microsoft.Practices.Unity.Interception" />
            </extensions>
            <extensionConfig>
              <add name="interception" type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationElement, Microsoft.Practices.Unity.Interception.Configuration"></add>
            </extensionConfig>
          </container>
        </containers>
      </MyUnityContainer>
   </configuration>

我需要在应用程序启动时加载它,但不会创建用户的配置文件!如果我的程序尚不存在,如何让我的程序在启动期间自动创建用户特定的配置?

Can someone give me a simple example on how to user user-settings instead of application-settings? I need to have user-specific Microsoft Unity section, but the config won't be created for the user on application startup. Also, I can't use the Visual Studio gui to create those settings. I need to modify some of the mappings during runtime as well.

This is what I need in the User-Config (which should be placed in the user's appdata)

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="MyUnityContainer" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
      </configSections>
      <connectionStrings>
        <add name="MyProgram.Properties.Settings.MyConnectionString" connectionString="aConnectionString" />
      </connectionStrings>
      <MyUnityContainer>
        <typeAliases>
          <!-- type aliases -->
        </typeAliases>
        <containers>
          <container>
            <types>
              <!-- types -->
            </types>
            <extensions>
              <add type="Microsoft.Practices.Unity.InterceptionExtension.Interception, Microsoft.Practices.Unity.Interception" />
            </extensions>
            <extensionConfig>
              <add name="interception" type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationElement, Microsoft.Practices.Unity.Interception.Configuration"></add>
            </extensionConfig>
          </container>
        </containers>
      </MyUnityContainer>
   </configuration>

I need to load this on application startup, but the config file for the user will not be created! How can I make my program create the user-specific config automatically during startup, if it does not exist already?

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

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

发布评论

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

评论(3

十年不长 2024-10-28 00:03:01

App.config 存储在应用程序根目录中,而 user.config 存储在用户配置文件中。 User.config 覆盖 App.config 设置

App.config is stored in application root while user.config is stored in user profile. User.config overrides the App.config settings

难如初 2024-10-28 00:03:01

我认为 user.config 覆盖仅适用于 appSetting 配置部分。

所以你会:

<appSettings file="user.config">

I thought user.config overrides were only available for the appSetting configuration section.

so you would have:

<appSettings file="user.config">
一指流沙 2024-10-28 00:03:01

我通过将整个配置复制到应用程序数据并通过路径映射从那里加载它来解决这个问题。这有效。

ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = exeFilePath; //somewhere in appdata in my case
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile,ConfigurationUserLevel.None);

I solved this by copying the whole config to app data and load it from there via a path mapping. This works.

ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = exeFilePath; //somewhere in appdata in my case
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile,ConfigurationUserLevel.None);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文