本文實例講述了C#中winform使用相對路徑讀取文件的方法。分享給大家供大家參考。具體分析如下:
目錄結構如下圖所示:
方法一:由于生成的exe文件在bin/debug目錄下,可以使用向上查找目錄的方式獲取要讀取的xml文件
FileInfo file = new FileInfo(fileName);
string fullName = file.FullName;
方法二:獲取exe文件的路徑進行截取,分兩次進行,然后拼接文件名,形成全路徑
string fullName = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("http://"));
fullName = fullName.Substring(0, fullName.LastIndexOf("http://")) + "http://" + haarXmlPath;
另一種方式:
希望本文所述對大家的C#程序設計有所幫助。
新聞熱點
疑難解答