[英]How to read file from the installation folder of app using c#?
我正在使用File.ReadAllText(“filename.txt”)讀取文件。 此文件位於.exe文件所在的文件夾(c:/ program files / installation folder)中。 但默認情況下,Windows應用程序會查看此文件的System32文件夾。
**我不想硬編碼文件的路徑。
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
給出了exe的目錄。 因此,使用Path.Combine將獲得所需的結果:
string filenamelocation = System.IO.Path.Combine(path, "filename.txt");
試試這個功能:
using System.Reflection;
private string MyDirectory()
{
return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
}
然后用
File.ReadAllText(MyDirectory() + @"\filename.txt");
短得多
File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "filename.txt");
那么您需要應用程序的目錄嗎? 在這種情況下,SO上已有一些好的答案,例如:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.