Often times, SBerry is biwildered as to how to access files during file handling in programs. It has find many and many usecases making it both clear and confused at tandem. Wanting to have a concrete understanding of the file handling concepts, here comes today’s blog post.

The main difficulty with file handling is to locate and access the file in the project directories. Though there are bunch of options to reach to the file, there is no proper ordered documented way to all these options.
Locating the files in console programs.

First let’s see how we can retrieve the current project executable folder.

  1. AppDomain.CurrentDomain.BaseDirectory
  2. Directory.GetCurrentDirectory();
  3. Environment.CurrentDirectory;

To get the directory along with the executable use System.Reflection.Assembly.GetExecutingAssembly.Location

If we want to retrieve any file location from the folder of the current running project i.e the folder in which the executable of the project resides,

  1. string newPath = Path.GetFullPath(Path.Combine(path, @”..\..\..\”));
  2. To navigate to the a file from a particular folder, use

string folderPath = @”..\..\..\Resources\Demo.txt”;

Happy Learning 🙂