Author Topic: C# program to list all files from a folder recursively  (Read 1867 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
C# program to list all files from a folder recursively
« on: December 01, 2011, 06:24:01 pm »
In the following C# code, Directory.GetFiles("c:\\kathir", "*", SearchOption.AllDirectories) will list all the files from the folder c:\kathir recursively.
You can use SearchOption.TopDirectoryOnly to remove the recursion and display the content of c:\kathir only.

Code: (C#)
//Source Code Listing

using System;
using System.IO;

namespace ListFiles
{
   class Program
   {
     static void Main(string[] args)
      {
         string[] files = Directory.GetFiles("c:\\kathir", "*", SearchOption.AllDirectories);

         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