C# ConfigurationManager Use records
The most recent ancestral code is using .NET Fx Written , I had some trouble getting configuration using the console program .
Here are some gestures for managing profiles :
ConfigurationManager Used to get configuration information in the client application ;
about web project , Please use WebConfigurationManager class .
ConfigurationManager Using posture
- add to app.config file
<configuration>
<appSettings>
<add key="ProjectName" value="cvg.java.api.productcenter" />
</appSettings>
<connectionStrings>
<add name="DBConnection" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=WingtipToys;Integrated Security=True;Pooling=False"/>
</connectionStrings>
</configuration>
- Be careful : After the compilation app.config The configuration section will enter the configuration file of the executable
Demo.exe.config ConfigurationManager.AppSettings["key1"]、ConfigurationManager.ConnectionStrings["DBConnection"]Used to get the program configuration from the default configuration of the application 、 Connection string configuration , This is also ConfigurationManager The most common usage .
- How to read external configuration ?
Putting all the configuration information in one configuration file is very confusing , especially [ Password management ] When , Multiple configuration files may be divided .
ConfigurationManager Support the creation of additional configuration files in the project .
------ app.config file -----
<configuration>
<connectionStrings configSource="DBConnectionStrings.config" />
</configuration>
----- DBConnectionString.config file , We don't need that anymore configuration Top level configuration section ----
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
<add name="DBConnection" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=WingtipToys;Integrated Security=True;Pooling=False" />
</connectionStrings>
The attached file will not go into Demo.exe.config file , You can imagine , When you need to hide the file configuration , It is not necessary to add this file to code management .
- ConfigurationManager Support Machine,User,Exe Three levels of configuration files , Can pass
ExeConfigurationFileMapLoad the configuration file for a specific location .
var configFileMap = new ExeConfigurationFileMap()
{
ExeConfigFilename = @"E:\Test\WpfApp2\bin\Debug\PositionConfig.config"
};
var v = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
Let's take a look at how Microsoft writes tool library files ,ConfigurationManager Is a static class , Static constructor ,
Using static methods AppSettings["key1"] Index configuration , You must first make sure that the configuration file is ready , Note the following PrepareConfigSystem==>EnsureConfigurationSystem Method
public static object GetSection(string sectionName)
{
if (string.IsNullOrEmpty(sectionName))
{
return null;
}
PrepareConfigSystem();
return s_configSystem.GetSection(sectionName);
}
private static void PrepareConfigSystem()
{
if (s_initState < InitState.Usable)
{
EnsureConfigurationSystem();
}
if (s_initError != null)
{
throw s_initError;
}
}
A status field is used to represent the initialization process , Notice that a lock Prevent multiple initializations under concurrency
private static void EnsureConfigurationSystem() {
// If a configuration system has not yet been set,
// create the DefaultConfigurationSystem for exe's.
lock (s_initLock) {
if (s_initState < InitState.Usable) {
s_initState = InitState.Started;
try {
try {
s_configSystem = new ClientConfigurationSystem();
s_initState = InitState.Usable;
}
catch (Exception e) {
s_initError = new ConfigurationErrorsException(SR.GetString(SR.Config_client_config_init_error), e);
throw s_initError;
}
}
catch {
s_initState = InitState.Completed;
throw;
}
}
}
}
This article is a brief technical flash , Recorded ConfigurationManager And the general development mode of Microsoft tool library .
,





![[genius_platform software platform development] lecture 37: network card hybrid mode and raw socket](/img/bf/880fbf4122b66723b6e17c6d9d97c9.jpg)

![[PHP] brief description and relevant examples of the special class trail for code reuse](/img/6f/fe3b93661276f44d0286199ed6d643.png)

