Getting path to the parent folder of the solution file using C#

User1204501 picture User1204501 · Sep 25, 2013 · Viewed 62.7k times · Source

I am a beginner in C#, and I have a folder from which I am reading a file.

I want to read a file which is located at the parent folder of the solution file. How do I do this?

string path = "";
StreamReader sr = new StreamReader(path);

So if my file XXX.sln is in C:\X0\A\XXX\ then read the .txt files in C:\X0\A\.

Answer

Thilina H picture Thilina H · Sep 25, 2013

Try this:

string startupPath = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName,"abc.txt");

// Read the file as one string. 
string text = System.IO.File.ReadAllText(startupPath);