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.
|