ConfigurationManager讀取dll的配置文件

ConfigurationManager讀取dll的配置文件

  最近一個項目,須要發佈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;
相關文章
相關標籤/搜索