Working with Configuration Files Outside of Assemblies in .NET
.NET applications typically use ConfigurationManager.OpenExe(exePath)
to access configuration files linked to specific assemblies. But what if your config file isn't directly associated with an assembly?
The solution lies in using the ExeConfigurationFileMap
class. This allows you to create a direct link between your application and an external configuration file. Here's how:
<code class="language-csharp">ExeConfigurationFileMap configMap = new ExeConfigurationFileMap(); configMap.ExeConfigFilename = @"d:\test\justAConfigFile.config.whateverYouLikeExtension"; Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);</code>
ExeConfigFilename
specifies the complete path to your configuration file. ConfigurationUserLevel.None
ensures the configuration applies to all users.
Accessing settings is straightforward:
<code class="language-csharp">config.AppSettings.Settings["test"].Value;</code>
This retrieves the value associated with the "test" key within the AppSettings
section of your specified configuration file.
The above is the detailed content of How to Access Non-Assembly-Specific Configuration Files in .NET?. For more information, please follow other related articles on the PHP Chinese website!