Get Directory Listing and Current Application Directory in C#

Recursive directory listing in C# and get a list of all the files and folders in the directory, returning them as a StringBuilder object.

By Tim TrottC# ASP.Net MVC • May 26, 2011
Get Directory Listing and Current Application Directory in C#

The function to get a directory listing in C# takes one parameter, the directory to start looking in. It will then retrieve a list of files and folders in that directory and for each folder it finds, it will call itself on that folder.

Each time the function finds files or folders it will add them to a StringBuilder object and return the directory listing to the calling function. If that function is itself, then the new directory listing is appended to the existing listing. This process repeats until all files and folders have been listed. The function then returns the completed recursive directory listing in C# back to the originating method, in the example shown, the main function.

Simple Directory Listing in C#

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      StringBuilder dirList = directoryListing("C:Inetpub", "");
      Console.WriteLine(dirList.ToString());
    }

    static StringBuilder directoryListing(string path, string indent)
    {
      StringBuilder result = new StringBuilder();
      DirectoryInfo di = new DirectoryInfo(path);
      DirectoryInfo[] rgDirs = di.GetDirectories();

      foreach (DirectoryInfo dir in rgDirs)
      {
        result.AppendLine(indent + dir.Name);
        result.Append(directoryListing(path, indent + "..").ToString());
      }
      return result;
    }
  }
}

If you want, you could adapt this directory listing in C# to return a List with the full path of the file if you need to access or process files, or you could use this method to recursively search for a file or folder. Why not let me know what you come up with in the comments section - and share your code to help others looking for similar tasks?

How to Obtain the Current Application Directory in C#

How to get the current application directory for where a program is being run. Useful if you need to load resources from the app folder. From time to time, you may need to access a file within the current application directory. .Net provides a property that is set to the absolute path to the application executable, and a method can be used to extract the folder name.

C#
using System.IO;
using System.Windows.Forms;

string appPath = Path.GetDirectoryName(Application.ExecutablePath);

Console Application project types will have to manually add a reference to the System.Windows.Forms assembly for the Application object to be exposed.

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

This post has 4 comments. Why not join the discussion!

New comments for this post are currently closed.