簡體   English   中英

如何創建相對文件路徑

[英]How to create a Relative File Path

我有一個鏈接,我正在閱讀流閱讀器 - @“C:\\ Users \\ James \\ Desktop \\ tweets.txt”

我將文本文件放在與源代碼和.exe相同的文件夾中

我如何將'@“C:\\ Users \\ James \\ Desktop \\ tweets.txt”'更改為相對文件路徑?

您可以使用Assembly.GetExecutingAssembly().Location獲取當前的程序集/ exe路徑。 完成后,您可以輕松地將文本文件名附加到路徑中以訪問文本文件

您還可以使用System.IO.Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location)獲取包含您的exe的文件夾的路徑System.IO.Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location)

正如Boo所說,你可以做到

Path.Combine(System.IO.Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location), "Text File Name");

你的意思是相對路徑? 你可以試試這個:

Uri srFile = new Uri(@"C:\Users\James\Desktop\tweets.txt");

Uri projFile= new Uri(@"C:\MyprojectDirectory\Project\...");



Uri relativePath = projFile.MakeRelativeUri(srFile);

您可以使用Application.StartupPath屬性:

using System.Windows.Forms;
...
string myApplicationPath = Application.StartupPath;

暫無
暫無

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

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