在网上大多数的例子都是如何访问程序运行的目录下的配制文件,但是U9BP里要将DLL放到生产环境,我并不知道生产环境运行的目录,所以只能DLL读取当前目录XML。

过下面几个方法都可以拿到程序所在的文件夹或程序文件

  • AppDomain.CurrentDomain.BaseDirectory 当前程序域寻找 dll 的文件夹
  • Environment.CurrentDirectory 当前工作文件夹
  • Assembly.GetCallingAssembly().Location 调用当前函数的函数的程序集的文件
  • Assembly.GetEntryAssembly().Location 入口函数程序集所在的文件
  • Assembly.GetExecutingAssembly().Location 包含当前代码的程序集的文件
  • Directory.GetCurrentDirectory 当前工作文件夹

所以我当然选第一种 AppDomain.CurrentDomain.BaseDirectory

XML文件

<?xml version="1.0" encoding="utf-8" ?> <Configure> <portConfigure> <url></url> <tokenUrl></tokenUrl> <user></user> <password></password> </portConfigure> </Configure>

 

//读取网站下XML文件CS代码
string
path = AppDomain.CurrentDomain.BaseDirectory+"XXX.xml"; XmlDocument xml = new XmlDocument(); //打开一个xml xml.Load(path); //获取portConfigure配置节点 XmlNode portConfigure = xml.SelectSingleNode("Configure/portConfigure"); //读取节点数据

string xxx= portConfigure.SelectSingleNode("url").InnerText; //获取节点
 

 

 

posted on 2019-07-05 11:55  Xanthus博客  阅读(362)  评论(0编辑  收藏  举报