如何使用c#从应用程序的安装文件夹中读取文件?

如何使用c#从应用程序的安装文件夹中读取文件?,c#,windows,windows-services,C#,Windows,Windows Services,我正在使用file.ReadAllText(“filename.txt”)读取一个文件。此文件位于.exe文件所在的文件夹中(c:/program files/installation文件夹)。但默认情况下,windows应用程序会查找此文件的System32文件夹 **我不想硬编码文件的路径 string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetNam

我正在使用file.ReadAllText(“filename.txt”)读取一个文件。此文件位于.exe文件所在的文件夹中(c:/program files/installation文件夹)。但默认情况下,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");

如何启动此
exe
?可能有什么东西修改了当前的工作目录?它是一个windows服务…总是在运行…只是计划根据指定的时间调用某些任务。