List all files in a directory and subdirectories with C#
This post will discuss how to recursively list all files in a directory and all its subdirectories in C#.
1. Using GetFiles
and GetDirectories
methods
To get the list of full names of files and subdirectories in the specified directory, we can use GetFiles and GetDirectories() methods in the System.IO.Directory
class, as shown below:
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 rootdir = @"C:\some\path"; // get the list of files string[] files = Directory.GetFiles(rootdir); Console.WriteLine(String.Join(Environment.NewLine, files)); // get the list of directories string[] dirs = Directory.GetDirectories(rootdir); Console.WriteLine(String.Join(Environment.NewLine, dirs)); } } |
The above-mentioned methods are overloaded to accept the search pattern and search options. If the search pattern is specified, the method matches the pattern against the names of files and directories in the path. The search options specify whether the search should include all subdirectories or only the root directory.
The following code uses the *
pattern and SearchOption.AllDirectories
option to retrieve all types of files in the current directory and its subdirectories.
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 rootdir = @"C:\some\path"; // get the list of files in the root directory and all its subdirectories string[] files = Directory.GetFiles(rootdir, "*", SearchOption.AllDirectories); Console.WriteLine(String.Join(Environment.NewLine, files)); // get the list of directories and subdirectories string[] dirs = Directory.GetDirectories(rootdir, "*", SearchOption.AllDirectories); Console.WriteLine(String.Join(Environment.NewLine, dirs)); } } |
2. Using EnumerateFiles
and EnumerateDirectories
methods
Alternatively, we can use EnumerateFiles and EnumerateDirectories to get an enumerable collection of full names of files and subdirectories in the specified directory.
The following example shows how to retrieve all files in a directory and its subdirectories.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
using System; using System.IO; using System.Collections.Generic; public class Example { public static void Main() { string rootdir = @"C:\some\path"; // print list of files in the root directory and all its subdirectories var files = Directory.EnumerateFiles(rootdir, "*", SearchOption.AllDirectories); Console.WriteLine(String.Join(Environment.NewLine, files)); // print list of directories and subdirectories var dirs = Directory.EnumerateDirectories(rootdir, "*", SearchOption.AllDirectories); Console.WriteLine(String.Join(Environment.NewLine, dirs)); } } |
3. Using Directory.GetFileSystemEntries()
method
Another idea is to use the Directory.GetFileSystemEntries() method returns the names of all files and subdirectories in the specified path. It is overloaded to accept search patterns and search options. If a search pattern is specified, the method matches the pattern against the names of files and directories in the path. If the SearchOption.AllDirectories
option is specified, it searches inside all subdirectories.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using System.IO; public class Example { public static void Main() { string rootdir = @"C:\some\path"; string[] files = Directory.GetFileSystemEntries(rootdir, "*", SearchOption.AllDirectories); Console.WriteLine(String.Join(Environment.NewLine, files)); } } |
That’s all about listing all files in a directory and subdirectories with 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 :)