ConfigurationManager读取dll的配置文件

ConfigurationManager读取dll的配置文件

  最近一个项目,需要发布dll给第三方使用,其中需要一些配置参数。

我们知道.NET的exe工程是自带的App.config文件的,编译之后会生成XX.exe.config文件,

使用静态类ConfigurationManager即可读取。

                string ip = ConfigurationManager.AppSettings["ServerIP"];
                int port = Convert.ToInt32(ConfigurationManager.AppSettings["ServerPort"]);

  但是一个类库工程生成的Dll能否读取相关的配置文件呢,答案是可以的。不需要我们自己写XML配置文件读取。

还是使用静态类ConfigurationManager,利用方法OpenExeConfiguration加载config文件。

注意:OpenExeConfiguration默认是直接加载dll路径,加载的时候会自动添加上扩展名.config。

如我们的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;



原文地址:https://www.cnblogs.com/linuxjava01/p/3736191.html