Working with Independent .NET Configuration Files
Problem:
How do I access a standalone .NET configuration file that's not linked to a specific assembly?
Solution:
The ExeConfigurationFileMap
class provides the solution. Here's how to open and read from a custom configuration file:
<code class="language-csharp">ExeConfigurationFileMap configMap = new ExeConfigurationFileMap(); configMap.ExeConfigFilename = @"d:\test\justAConfigFile.config.whateverYouLikeExtension"; // Replace with your file path Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);</code>
Remember to replace the placeholder path with the correct location of your configuration file. The extension can be anything you choose.
Accessing settings is straightforward using the indexer:
<code class="language-csharp">string value = config.AppSettings.Settings["test"].Value;</code>
This retrieves the value associated with the key "test" within the AppSettings
section of your configuration file.
The above is the detailed content of How Can I Access a Custom .NET Configuration File?. For more information, please follow other related articles on the PHP Chinese website!