List all files in a directory in C#
This post will discuss how to list all files in a directory in C#.
1. Using Directory.GetFiles() method
You can use the Directory.GetFiles() method to get the list of files in the specified directory. It takes the relative or absolute path of the directory to search, and returns a string array containing the file names (including their paths) in the specified directory, in no fixed order.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.IO; public class Example { public static void Main() { string path = @"C:\Dir"; string[] files = Directory.GetFiles(path); foreach (string file in files) { Console.WriteLine(file); } } } |
The Directory.GetFiles() method is overloaded to accept the search pattern to match against the files in the specified directory. For example, the following code will list out all the files in the specified directory with the .jpg extension.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.IO; public class Example { public static void Main() { string path = @"C:\Dir"; string[] files = Directory.GetFiles(path, "*.jpg"); foreach (string file in files) { Console.WriteLine(file); } } } |
By default, the Directory.GetFiles() method performs the search only in the current directory. To extend the search operation to include all its subdirectories, you can provide the SearchOption.AllDirectories option as the third parameter. For example, the following code will list all the files present in the specified directory and all its subdirectories with any extension (asterisk (*) matches with any file type).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.IO; public class Example { public static void Main() { string path = @"C:\Dir"; string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories); foreach (string file in files) { Console.WriteLine(file); } } } |
If you need to get the names of all files along with all the names of all its subdirectories, consider using the Directory.GetFileSystemEntries method instead. For example, the following code will recursively list out all the files and subdirectories present in the specified directory with any extension.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.IO; public class Example { public static void Main() { string path = @"C:\Users\adity\OneDrive\Desktop\New folder"; string[] files = Directory.GetFileSystemEntries(path, "*", SearchOption.AllDirectories); foreach (string file in files) { Console.WriteLine(file); } } } |
2. Using DirectoryInfo.GetFiles() method
Alternatively, you can use the DirectoryInfo.GetFiles() method to return a list of files in the current directory. Here’s a working code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.IO; public class Example { public static void Main() { string path = @"C:\Dir"; DirectoryInfo directory = new DirectoryInfo(path); FileInfo[] files = directory.GetFiles(); foreach (FileInfo file in files) { Console.WriteLine(file.Name); } } } |
Like The Directory.GetFiles(), the DirectoryInfo.GetFiles() method is overloaded to accept the search pattern. The following example will return all the files in the specified directory with the .png extension.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.IO; public class Example { public static void Main() { string path = @"C:\Dir"; DirectoryInfo directory = new DirectoryInfo(path); FileInfo[] files = directory.GetFiles("*.png"); foreach (FileInfo file in files) { Console.WriteLine(file.Name); } } } |
You can extend the search operation to all subdirectories by specifying the SearchOption.AllDirectories option. The following code will list all the files in the specified directory and all its subdirectories.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.IO; public class Example { public static void Main() { string path = @"C:\Dir"; DirectoryInfo directory = new DirectoryInfo(path); FileInfo[] files = directory.GetFiles("*", SearchOption.AllDirectories); foreach (FileInfo file in files) { Console.WriteLine(file.Name); } } } |
3. Custom Routine
Finally, you can even write your custom routine to extend the search to include all subdirectories of the source directory, without having to use the SearchOption.AllDirectories option in the Directory.GetFiles() method. The idea is to iterate over files in the current directory and process them. If the directory contains any subdirectories, do this recursively.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
using System; using System.IO; public class Example { public static void WalkDirectory(string path) { foreach (string file in Directory.GetFiles(path)) { Console.WriteLine(file); } foreach (string dir in Directory.GetDirectories(path)) { WalkDirectory(dir); } } public static void Main() { string path = @"C:\Dir"; WalkDirectory(path); } } |
That’s all about listing all files in a directory in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)