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 Trott | C# ASP.Net MVC | May 26, 2011

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.

Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

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 comment(s). Why not join the discussion!

We respect your privacy, and will not make your email public. Learn how your comment data is processed.

  1. SU

    On Friday 11th of May 2012, Suneel said

    Some other way to get current directory path.

    1) Application.StartupPath
    2) Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
    3) AppDomain.CurrentDomain.BaseDirectory
    4) Path.GetDirectoryName( Environment.GetCommandLineArgs()[0])
    5) System.IO.Directory.GetCurrentDirectory()
    6) Environment.CurrentDirectory

  2. HE

    On Wednesday 18th of May 2011, HELLIC said

    There is no App.path in C#, that is current working Folder of project.

    This is rediculous library provided by Microsoft.
    Helll is well

  3. KY

    On Tuesday 6th of July 2010, Kyle said

    Hey, thanks for the help! :) Finally was able to continue on coding!

    Thanks
    ~Kyle

  4. SA

    On Thursday 10th of September 2009, sandrar said

    Hi! I was surfing and found your blog post... nice! I love your blog. :) Cheers! Sandra. R.