Search code examples
c#directorypathfilepath

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


I am doing a program which reads .txt files and I want to access those without having to change manually the filePath directory. I have it like this: "F:\TR\AppPathFinding\AppPathFinding\bin\Debug" Here I have the .exe file, which starts the program. Secondly, I want to access one folder with different .txt files. It is here: "F:\TR\AppPathFinding\AppPathFinding\bin\Debug\GrillSelection" and the .txt name is: "SetGrill1.txt". How can I access this .txt without having to change the path manually?

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

This is what I need to change.

Someone can help me please?


Solution

  • As far as I understand you always want to access the same folder that is located at your .exe. Using a relative path.

    This is very well explained by Kieren Johnstone.

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