zoukankan      html  css  js  c++  java
  • 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;



  • 相关阅读:
    hdu 3832 Earth Hour
    hdu 2112 HDU Today
    hdu 2473 Junk-Mail Filter
    hdu 1558 Segment set
    5-2学习记录|5-3日
    看面经
    2020-5-1学习记录
    2020-4-30学习记录
    2020-4-27学习记录
    4.19日学习笔记
  • 原文地址:https://www.cnblogs.com/linuxjava01/p/3736191.html
Copyright © 2011-2022 走看看