簡體   English   中英

將相對路徑轉換為絕對路徑

[英]Convert Relative Path to Absolute Path

我正在嘗試使用linkLabel在Windows窗體中打開Help.txt文件。 但是無法從絕對路徑轉換為相對路徑。

首先,我嘗試獲取exe文件的絕對路徑。 哪個成功。 其次,僅獲取exe文件的目錄。 哪個成功。 第三,我試圖將目錄與Help.txt文件的相對路徑結合在一起。 這是不成功的。

Exe文件位於-> \\ Project \\ bin \\ Debug文件夾中,但是Help.txt文件位於\\ Project \\ Help文件夾中。 這是我的代碼:

 string exeFile = (new System.Uri(Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;
 string Dir = Uri.UnescapeDataString(Path.GetDirectoryName(exeFile));
 string path = Path.Combine(Dir, @"..\..\Help\Help.txt");
 System.Diagnostics.Process.Start(path);

我的路徑的結果是-> \\ Project \\ bin \\ Debug .... \\ Help \\ Help.txt

您需要使用Path.GetFullPath()來考慮上層目錄“ ../../”,如下所示:

string exeFile = new System.Uri(Assembly.GetEntryAssembly().CodeBase).AbsolutePath;
string Dir = Path.GetDirectoryName(exeFile);
string path = Path.GetFullPath(Path.Combine(Dir, @"..\..\Help\Help.txt"));
System.Diagnostics.Process.Start(path);

根據GetFullPath的MSDN:返回指定路徑字符串的絕對路徑。 Path.Combine將字符串組合到路徑中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM