C# – How to read existing text files without defining path

cfile-ionettext;

Most of the examples shows how to read text file from exact location (f.e. "C:\Users\Owner\Documents\test1.txt"). But, how to read text files without writing full path, so my code would work when copied to other computers. With visual studio I added 2 text files to project (console project) and don't know best way to read those files. Hope I described my problem clearly. Maybe I needed to add those txt files differentely (like directly to same folder as .exe file)?

Best Answer

You could use Directory.GetCurrentDirectory:

var path = Path.Combine(Directory.GetCurrentDirectory(), "\\fileName.txt");

Which will look for the file fileName.txt in the current directory of the application.