Read application/web configuration settings in .NET
When developing a C# class library, you need to read configuration settings from the app.config or web.config file according to the deployment scenario. This article explores best practices for accessing these settings, including the limitations of deprecated methods and the availability of newer alternatives.
Earlier ConfigurationSettings.AppSettings.Get()
methods are obsolete. Recommended alternative ConfigurationManager.AppSettings["MySetting"]
provides enhanced support and stability. However, this class is not directly accessible in C# class library projects.
The solution is to add a reference to the System.Configuration
assembly in your project. This makes it possible to use the ConfigurationManager
class and retrieve configuration settings like this:
<code class="language-csharp">using System.Configuration; string configValue1 = ConfigurationManager.AppSettings["countoffiles"]; string configValue2 = ConfigurationManager.AppSettings["logfilelocation"];</code>
The above is the detailed content of How to Efficiently Read App/Web Configuration Settings in a .NET C# Class Library?. For more information, please follow other related articles on the PHP Chinese website!