how to read .txt file next to the Program.cs
i have the following code that is begin run
the rest of the code is not relevant since this problem even accurs when this is the first line of the whole program.
but then when I run this code it returns
Unhandled exception. System.IO.FileNotFoundException: Could not find file '..\server\bin\Debug\net6.0\hamlet.txt'
However, the code itself is all located in the folder called server
so it should actually try to read server\hamlet.txt
What might be relevant is that i am running the it with Jetbrains Rider.
and so a dirty fix would be to append ../../../
to the path, but i feel like this is the worst possible fix10 Replies
but it's the compiled program that it's trying to read the text file, not the source code, because that's just text
and the compiled program is in the bin folder
so the text file in the source folder would be a "resource" and should be copied in the bin /... folder too
which you could set using the properties of the text file itself (inside the ide)
aha okay thanks!
so if i understand correctly there are 2 ways of doing this
1. the dirty fix: adding
../../../
to the path
2. the propper fix: copying over the file to the bin directory (next to the executable)i am not sure if i am doing it right, i set it to resource but when i build it, its still not in the bin/.. folder
well do you see the compilation log saying the file is copied?
I have honestly no idea where i see that
I thought it recompled whenever you changed something in the program code?
i mean, if its the build stuff where it also sais something about
server -> ...\server\bin\Debug\net6.0\server.dll
, then it sais nothing about a test file called hamlet.txti use visual studio so i don't exactly know where rider keeps the logs
but you can still
dotnet build
from console
that will give some logs; maybe try with a clean firstJust make sure the file is copied on build
It's a couple of lines in the csproj file
Ah, nevermind, I see the issue
Don't set it to be a resource. Just a regular ol' file
wait sorry i dont understand?
also, if i add it like so
and then run
dontnet build
then it does Build successfull but still does not copy it over
how would i approach it then? what is the comman way people do it?
This will copy it as a file, instead of embedding it as a resource
oh thnx that works indeed
but does this mean i should do it this way?
oh wait, no i see what you mean with not making it a resource
(since copyTo... was first encapsulated in the resource tag, and now in the none tag)