[英]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.