Author Topic: C# program to list all directories with recursion  (Read 1275 times)


  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
C# program to list all directories with recursion
« on: December 01, 2011, 06:28:30 pm »
In the following C# code, Directory.GetDirectories("c:\\kathir", "b*", SearchOption.TopDirectoryOnly) will list all the files from the folder c:\kathir.
You can use SearchOption.AllDirectories to include the recursion and display the content of c:\kathir and its sub directories.

Code: (C#)
//Source Code Listing

using System;
using System.IO;
namespace ListDirectory
   class Program
   static void Main(string[] args)
         string[] arrTempSubDirs = Directory.GetDirectories("c:\\kathir", "b*", SearchOption.TopDirectoryOnly);
         foreach (string dir in arrTempSubDirs)
         Console.WriteLine("Total number of files found: {0}", arrTempSubDirs.Length);

         string[] files = Directory.GetFiles("c:\\kathir", "*", SearchOption.TopDirectoryOnly);

         foreach (string file in files)
            Console.WriteLine(file); // prints the complete the path
         Console.WriteLine("Total number of files found: {0}", files.Length);


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1