ConfigurationManager姿势快闪
C# ConfigurationManager使用记录
最近一个祖传代码是使用.NET Fx写就的,我在使用控制台程序获取配置时有些折腾。
下面记录一些管理配置文件的姿势:
ConfigurationManager
用于在客户机应用程序中获取配置信息;
对于web项目,请使用WebConfigurationManager
类。
ConfigurationManager使用姿势
- 添加app.config文件
<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>
- 注意:编译之后app.config配置节会进入可执行文件的配置文件
Demo.exe.config
ConfigurationManager.AppSettings["key1"]
、
ConfigurationManager.ConnectionStrings["DBConnection"]
用于从应用的默认配置中获取程序配置、连接字符串配置, 这也是ConfigurationManager最常规的用法。
- 如何读取外部配置?
将所有配置信息放在一个配置文件显得非常混乱,特别是[密码管理]的时候, 可能会划分多个配置文件。
ConfigurationManager支持项目中创建另外的配置文件。
------ app.config文件----- <configuration> <connectionStrings configSource="DBConnectionStrings.config" /> </configuration> ----- DBConnectionString.config文件, 这里已经不需要configuration顶级配置节---- <?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>
附加的这个文件不会进Demo.exe.config文件,可以想象到,当需要隐藏该文件配置,可以不把该文件加入代码管理。
- ConfigurationManager支持Machine,User,Exe三个级别的配置文件, 可以通过
ExeConfigurationFileMap
加载特定位置的配置文件。
var configFileMap = new ExeConfigurationFileMap() { ExeConfigFilename = @"E:\Test\WpfApp2\bin\Debug\PositionConfig.config" }; var v = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
我们顺便看下微软是如何编写工具库文件,ConfigurationManager 是一个静态类,静态构造函数,
在使用静态方法 AppSettings["key1"]索引配置时,必须先确保配置文件已经就绪,注意下面的PrepareConfigSystem
==>EnsureConfigurationSystem
方法
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; } }
使用了一个状态字段来表征初始化过程, 注意这里使用了一个lock
防止并发下被多次初始化
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; } } } }
本文算是简短的技术快闪,记录了ConfigurationManager 的使用姿势和微软工具库的一般开发模式。
,
本文来自博客园,作者:{有态度的马甲},转载请注明原文链接:https://www.cnblogs.com/JulianHuang/p/16358221.html
欢迎关注我的原创技术、职场公众号, 加好友谈天说地,一起进化
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?