How to read a text file in project's root directory?

Shai UI picture Shai UI · Jun 20, 2011 · Viewed 172.8k times · Source

I want to read the first line of a text file that I added to the root directory of my project. Meaning, my solution explorer is showing the .txt file along side my .cs files in my project.

So, I tried to do:

TextReader tr = new StreamReader(@"myfile.txt");
string myText = tr.ReadLine();

But this doesn't work since it's referring to the Bin Folder and my file isn't in there... How can I make this work? :/

Thanks

Answer

dance2die picture dance2die · Jun 20, 2011

From Solution Explorer, right click on myfile.txt and choose "Properties"

From there, set the Build Action to content and Copy to Output Directory to either Copy always or Copy if newer

enter image description here