最近一个项目,须要发布dll给第三方使用,其中须要一些配置参数。spa
咱们知道.NET的exe工程是自带的App.config文件的,编译以后会生成XX.exe.config文件,code
使用静态类ConfigurationManager便可读取。blog
string ip = ConfigurationManager.AppSettings["ServerIP"]; int port = Convert.ToInt32(ConfigurationManager.AppSettings["ServerPort"]);
可是一个类库工程生成的Dll可否读取相关的配置文件呢,答案是能够的。不须要咱们本身写XML配置文件读取。ip
仍是使用静态类ConfigurationManager,利用方法OpenExeConfiguration加载config文件。string
注意:OpenExeConfiguration默认是直接加载dll路径,加载的时候会自动添加上扩展名.config。io
如咱们的Dll是XX.dll,相应的config文件是XX.dll.config.编译
Configuration AppConfig = ConfigurationManager.OpenExeConfiguration("XX.dll"); string strServerName = AppConfig.AppSettings.Settings["ServerName"].Value; string strServerPath = AppConfig.AppSettings.Settings["ServerPath"].Value;