Jan*_*ngo 3 c# windows windows-services
我正在使用File.ReadAllText("filename.txt")读取文件.此文件位于.exe文件所在的文件夹(c:/ program files/installation folder)中.但是默认情况下,Windows应用程序会查看此文件的System32文件夹.
**我不想硬编码文件的路径.
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
Run Code Online (Sandbox Code Playgroud)
给出了exe的目录.因此,使用Path.Combine将获得所需的结果:
string filenamelocation = System.IO.Path.Combine(path, "filename.txt");
Run Code Online (Sandbox Code Playgroud)
试试这个功能:
using System.Reflection;
private string MyDirectory()
{
return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
}
Run Code Online (Sandbox Code Playgroud)
然后用
File.ReadAllText(MyDirectory() + @"\filename.txt");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8672 次 |
| 最近记录: |