Search code examples
c#rider

why does rider look for files in /bin/debug/net5.0


I try to use a txt file and my ide can t find it this is the stack trace

Unhandled exception. System.IO.FileNotFoundException: Could not find file 'C:\Users\Tudor\RiderProjects\WinterInternship2022-Backend-main\SantaClauseConsoleApp\SantaClauseConsoleApp\bi
n\Debug\net5.0\letter-template.txt'.
File name: 'C:\Users\Tudor\RiderProjects\WinterInternship2022-Backend-main\SantaClauseConsoleApp\SantaClauseConsoleApp\bin\Debug\net5.0\letter-template.txt'
   at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
   at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.StreamReader.ValidateArgsAndOpenPath(String path, Encoding encoding, Int32 bufferSize)
   at System.IO.StreamReader..ctor(String path)
   at SantaClauseConsoleApp.Letter.createTextLetter() in C:\Users\Tudor\RiderProjects\WinterInternship2022-Backend-main\SantaClauseConsoleApp\SantaClauseConsoleApp\Core\Letter.cs:line
28
   at SantaClauseConsoleApp.Program.Question2() in C:\Users\Tudor\RiderProjects\WinterInternship2022-Backend-main\SantaClauseConsoleApp\SantaClauseConsoleApp\Program.cs:line 87
   at SantaClauseConsoleApp.Program.Main(String[] args) in C:\Users\Tudor\RiderProjects\WinterInternship2022-Backend-main\SantaClauseConsoleApp\SantaClauseConsoleApp\Program.cs:line 12

And this is the calling of the file

StreamReader reader = new StreamReader("letter-template.txt");

I also tried with the absolute path there is no qeustion the problem is with the searching window although i don t know how to set it.


Solution

  • Because (it seems) you are running compiled in the debug mode (so output is in bin\Debug\net5.0\) and "letter-template.txt" will be searched as relative path with the output folder considered as the root.

    If "letter-template.txt" is added to the project you can make it to be automatically copied to the output directory - right click on it in solution explorer and set "Copy to output directory" option to "Copy if newer".

    See also: