Use user.config instead of app.config
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?
App.config is stored in application root while user.config is stored in user profile. User.config overrides the App.config settings
I thought user.config overrides were only available for the appSetting configuration section.
so you would have:
<appSettings file="user.config">
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);
链接地址: http://www.djcxy.com/p/68846.html