-1

My C# project needs to read a file that is in the same location as the executable. The file is under <PROJECT>/bin/x64/Debug/input.txt and under <PROJECT>/bin/x64/Release/input.txt. In the same folders there is also the program.exe.

When executing manually the program.exe from those directories it reads the file and executes successfully. When trying to run the executable from the visual studio it cannot find the file.

Is there a way to resolve it?

andreas
  • 157
  • 13

1 Answers1

2

I use

System.Environment.CurrentDirectory + @"\input.txt"

which returns a string of "C:\Users\username\source\repos\Solution_name\Project_name\bin\{DEBUG | RELEASE}\input.txt"

  • 1
    By default, a relative file path (e.g. `File.OpenRead("input.txt");`) would try to open `input.txt` in the same directory as `program.exe`. Building an absolute path like this works, of course, but it shouldn't be necessary and there simply isn't enough information provided in the question to know why it would be. Perhaps the working directory was changed in the project settings. – Lance U. Matthews Apr 17 '18 at 21:35