© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
20 replies
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'
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
server

so it should actually try to read
server\hamlet.txt
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
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

error CS1061, error CS0105, CS0103
C#CC# / help
2y ago
❔ Reading file (.txt)
C#CC# / help
3y ago
❔ Load txt file
C#CC# / help
3y ago
❔ Startup File to Program file
C#CC# / help
3y ago