源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C#中winform使用相对路径读取文件的方法

  • 时间:2022-03-02 23:42 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#中winform使用相对路径读取文件的方法
本文实例讲述了C#中winform使用相对路径读取文件的方法。分享给大家供大家参考。具体分析如下: 目录结构如下图所示: [img]http://files.jb51.net/file_images/article/201501/20151985712646.png?20150985736[/img]  [img]http://files.jb51.net/file_images/article/201501/20151985748042.png?2015099745[/img] 方法一:由于生成的exe文件在bin\debug目录下,可以使用向上查找目录的方式获取要读取的xml文件
[u]复制代码[/u] 代码如下:
string haarXmlPath = @"../../haarcascade_frontalface_alt_tree.xml"; FileInfo file = new FileInfo(fileName); string  fullName = file.FullName;
方法二:获取exe文件的路径进行截取,分两次进行,然后拼接文件名,形成全路径
[u]复制代码[/u] 代码如下:
string haarXmlPath = @"haarcascade_frontalface_alt_tree.xml"; string fullName = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")); fullName = fullName.Substring(0, fullName.LastIndexOf("\\")) + "\\" + haarXmlPath;
另一种方式:
[u]复制代码[/u] 代码如下:
/// <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(0, path.LastIndexOf("\\"));             folderName = path.Substring(path.LastIndexOf("\\") + 1);         }         return path.Substring(0, path.LastIndexOf("\\") + 1); }
希望本文所述对大家的C#程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部