使用 user.config 而不是 app.config
有人可以给我一个关于如何使用用户设置而不是应用程序设置的简单示例吗?我需要有特定于用户的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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
我认为 user.config 覆盖仅适用于 appSetting 配置部分。
所以你会:
I thought user.config overrides were only available for the appSetting configuration section.
so you would have:
我通过将整个配置复制到应用程序数据并通过路径映射从那里加载它来解决这个问题。这有效。
I solved this by copying the whole config to app data and load it from there via a path mapping. This works.