利用C# 怎么實現(xiàn)一個回到上一層目錄功能?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

其實很簡單也很無腦,但卻很實用,就是使用拆字符串的方法:
/// <summary>
/// 獲得項目的根路徑
/// </summary>
/// <returns></returns>
public string GetProjectRootPath()
{
string rootPath = "";
string BaseDirectoryPath = AppDomain.CurrentDomain.BaseDirectory; // F:\project\WPF\AstroATE-PDR\04. 程序\01. 源代碼\AstroATE\AstroATE\bin\Debug
// 向上回退三級,得到需要的目錄
rootPath = BaseDirectoryPath.Substring(0, BaseDirectoryPath.LastIndexOf("\\")); // 第一個\是轉義符,所以要寫兩個
rootPath = rootPath.Substring(0, rootPath.LastIndexOf(@"\")); // 或者寫成這種格式
rootPath = rootPath.Substring(0, rootPath.LastIndexOf("\\")); // @"F:\project\WPF\AstroATE-PDR\04. 程序\01. 源代碼\AstroATE\AstroATE
return rootPath;
}string str = GetProjectRootPath() + @"\data\幫助文檔.pdf"; //找到需要找的文件
好了,這樣就解決了。
補充:C# 如何獲取可執(zhí)行文件路徑的上上級目錄
DirectoryInfo di = new DirectoryInfo(string.Format(@"{0}..\..\", Application.StartupPath));
di.FullName..\有幾個就是往回退幾層
DirectoryInfo info = new DirectoryInfo(Application.StartupPath); String path = info.Parent.Parent.FullName;
string WantedPath = Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf(@"\"));
關于利用C# 怎么實現(xiàn)一個回到上一層目錄功能問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關知識。
名稱欄目:利用C#怎么實現(xiàn)一個回到上一層目錄功能-創(chuàng)新互聯(lián)
分享路徑:http://www.chinadenli.net/article12/dhosgc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、品牌網(wǎng)站制作、網(wǎng)站建設、網(wǎng)站導航、全網(wǎng)營銷推廣、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容