我們可以將工程中的一些配置常量放到App.config中
(1)添加App.config
Add -> New Item -> Visual C# Items -> Application Configuration File
(2)為config文件添加自定義配置節(jié)點
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Test" value="123"/>
</appSettings>
</configuration>
(3)為工程添加System.Configuration.dll的reference
Add References -> .NET -> System Configuration
(4)代碼中獲得自定義配置內(nèi)容
using System.Configuration;
ConfigurationManager.AppSettings["Test"]; //123
注:
如果Console工程引用了其他ClassLibrary工程瘟判,
而ClassLibrary中使用了ConfigurationManager帮寻,
則最終讀取的App.config文件是Console工程的揭鳞,而不是ClassLibrary工程洞渤。