一、C#中使用相对路径读取配置文件

一般Solution的目录结构如下图所示:

(如过看不到某些文件,可以点击 “显示所有文件” 图标)

方法一:由于生成的exe文件在bin\debug目录下,可以使用向上查找目录的方式获取要读取的xml文件

string tPath = @"../../Converts/XMLFile1.xml";
FileInfo file = new FileInfo(fileName);
string fullName = file.FullName;

注:使用@的意思是不转义/

方法二:获取exe文件的路径进行截取,分两次进行,然后拼接文件名,形成全路径

string tPath = @"XMLFile1.xml"; 
string fullName = Application.StartupPath.Substring(, Application.StartupPath.LastIndexOf("\\"));
fullName = fullName.Substring(, fullName.LastIndexOf("\\")) + "\\" + tPath;

方法三:

/// <summary>
/// 获取应用程序根路径
/// </summary>
private static string GetApplicationPath()
{
string path = Application.StartupPath;
//string path=AppDomain.CurrentDomain.BaseDirectory; //另一种获取方式
string folderName = String.Empty;
while (folderName.ToLower() != "bin")
{
path = path.Substring(, path.LastIndexOf("\\"));
folderName = path.Substring(path.LastIndexOf("\\") + );
}
return path.Substring(, path.LastIndexOf("\\") + );
}

最新文章

  1. Leetcode Anagrams
  2. python的tab自动补全
  3. bootstrap 分页
  4. mac 配置jdk maven
  5. 【HDOJ】4355 Party All the Time
  6. 疯狂的表单-html5新增表单元素和属性
  7. Android ListView A~Z快速索引(改进版)
  8. CSS 文章段落样式
  9. 关于oracle数据库(11)
  10. CodeForces 543D:Road Improvement
  11. SQL Update
  12. Gym101237C The Palindrome Extraction Manacher、SAM、倍增
  13. P1087 FBI树 二叉树
  14. genstr.py
  15. 数据库使用:sql server/mysql/sqlite
  16. [转]Sqoop-1.4.4工具import和export使用详解
  17. POJ_2533_Longest Ordered Subsequence
  18. TcMalloc的介绍以及Windows下安装使用
  19. bzoj 3289: Mato的文件管理 莫队+树状数组
  20. Repeater中服务器按钮

热门文章

  1. flask的migrate
  2. Linux 与 Unix 到底有什么不同?区别在哪?
  3. 匆忙记录 编译linux kernel zImage
  4. Mybatis之插件拦截
  5. 【ES6】import, require,export
  6. 【angularJS】过滤器
  7. 禁止 gVim 在 Linux 下自动生成 undo 文件 *.un~
  8. day09--函数的定义分类以及其使用(1)
  9. 环境部署(八):jenkins配置邮件通知
  10. keystone系列三:网关协议