C
C#8mo ago
WantToBeeMe

how to read .txt file next to the Program.cs

i have the following code that is begin run
string fileContent = File.ReadAllText("hamlet.txt");
Console.WriteLine(fileContent);
string fileContent = File.ReadAllText("hamlet.txt");
Console.WriteLine(fileContent);
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 fix
No description
10 Replies
Omnissiah
Omnissiah8mo ago
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)
WantToBeeMe
WantToBeeMeOP8mo ago
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)
WantToBeeMe
WantToBeeMeOP8mo ago
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
No description
Omnissiah
Omnissiah8mo ago
well do you see the compilation log saying the file is copied?
WantToBeeMe
WantToBeeMeOP8mo ago
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.txt
Omnissiah
Omnissiah8mo ago
i 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 first
Angius
Angius8mo ago
Just 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
WantToBeeMe
WantToBeeMeOP8mo ago
wait sorry i dont understand? also, if i add it like so
<ItemGroup>
<None Remove="hamlet.txt" />
<Resource Include="hamlet.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
</ItemGroup>
<ItemGroup>
<None Remove="hamlet.txt" />
<Resource Include="hamlet.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
</ItemGroup>
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?
Angius
Angius8mo ago
<ItemGroup>
<None Update="hamlet.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Update="hamlet.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
This will copy it as a file, instead of embedding it as a resource
WantToBeeMe
WantToBeeMeOP8mo ago
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)
Want results from more Discord servers?
Add your server