CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

How to get all files in the current directory and in all sub directories under the directory in C#?
Submitted By Bala Murugan
On 11/8/2010 8:53:27 AM
Tags: C#,CodeDigest  

How to get all files in the current directory and in all sub directories under the directory in C#?

 

Sometimes, we may get requirements to get all files under the directory and its subdirectories.

The GetFiles() method in Directory class can be used to fetch all the files under a directory. This method also has wildcard support to list only some specific files that matches the wildcard expression.

 

The following little code snippet will help us to achieve the same.

List all Files under a Directory using C#

string[] files = Directory.GetFiles(@"E:\Technicals\Samples", "*", SearchOption.AllDirectories);
Console.WriteLine("List of files");
foreach (string file in files)
        {
Console.WriteLine(file);
        }


The above code will print all the files contained in the directory(E:\Technicals\Samples) and its sub directories. The character "*" indicated the GetFiles() method should return all the files under the directory E:\Technicals\Samples. 

 

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..