如何创建取决于 .exe 位置的文件路径

How do I do a filePath wich depens on the location of the .exe

我正在编写一个读取 .txt 文件的程序,我想访问这些文件而无需手动更改 filePath 目录。我是这样写的:“F:\TR\AppPathFinding\AppPathFinding\bin\Debug” 这里我有启动程序的 .exe 文件。其次,我想访问一个包含不同 .txt 文件的文件夹。它在这里:“F:\TR\AppPathFinding\AppPathFinding\bin\Debug\GrillSelection”,.txt 名称是:“SetGrill1.txt”。 如何在不手动更改路径的情况下访问此 .txt?

filePath = @"F:\TR\AppPathFinding\AppPathFinding\bin\Debug\GrillSelection\SetGrill1.txt";

这是我需要改变的。

有人可以帮助我吗?

据我了解,您总是希望访问位于您的 .exe 中的同一个文件夹。使用相对路径。

Kieren Johnstone 很好地解释了这一点。

string filePath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\GrillSelection\SetGrill1.txt";