C# Directory.GetFiles Example (Get List of Files)

Use the Directory.GetFiles and EnumerateFiles methods from System.IO.
Directory.GetFiles. This returns the file names in a folder. It returns a string array—this contains the full paths of all the files contained inside the specified directory.File
With EnumerateFiles, another System.IO method, we can handle large directories faster. And the SearchOption.AllDirectories enum will recursively get file names.
GetFiles example. You must include the System.IO namespace with a using directive at the top of your file, or use the fully qualified name System.IO.Directory.GetFiles type.

Here: The program uses the C:\ directory. It filters the files in that directory and only displays the ones with the "BIN" extension.

Literals: More information about the string literal format used is available—the verbatim literals are useful for paths.

String Literal

Argument 1: The first argument to GetFiles is the directory path. In a Windows Forms program, you can use Environment.SpecialFolder.


Argument 2: The second argument uses the pattern "*.BIN". You have seen these patterns in the "Open File" dialogs in Windows before.

C# program that gets files in directories using System; using System.IO; class Program { static void Main() { // Put all file names in root directory into array. string[] array1 = Directory.GetFiles(@"C:\"); // Put all bin files in root directory into array. // ... This is case-insensitive. string[] array2 = Directory.GetFiles(@"C:\", "*.BIN"); // Display all files. Console.WriteLine("--- Files: ---"); foreach (string name in array1) { Console.WriteLine(name); } // Display all BIN files. Console.WriteLine("--- BIN Files: ---"); foreach (string name in array2) { Console.WriteLine(name); } } } Output --- Files: --- (All files in your C:\ folder) --- BIN Files: --- (All .bin, .BIN files in your C:\ folder)
List example. You can get a List collection of the file paths in a directory. First get an array from GetFiles. Then you can convert the array to a List with the ToList extension method.

Tip: Include the System.Linq namespace at the top first—it is included by default in new files.

ToListConvert List, ArrayList
C# program that gets list of files using System; using System.Collections.Generic; using System.IO; using System.Linq; class Program { static void Main() { string[] array1 = Directory.GetFiles(@"C:\"); // Get list of files. List<string> filesList = array1.ToList(); Console.WriteLine(filesList.Count); } } Output 14
EnumerateFiles. With EnumerateFiles, each file name is returned in an IEnumerable collection—so the entire string array of file names can be avoided.

Tip: EnumerateFiles can be faster for large result lists. But for small directories, GetFiles may be faster.

C# program that uses Directory.EnumerateFiles using System; using System.IO; class Program { static void Main() { // Loop over all files in C directory. foreach (string path in Directory.EnumerateFiles("C:\\")) { Console.WriteLine("IN C DIRECTORY: " + path); } } } Output IN C DIRECTORY: C:\pagefile.sys IN C DIRECTORY: ... IN C DIRECTORY: C:\swapfile.sys IN C DIRECTORY: ...
Recursive example. Often we need to get the list of files in a certain directory, and then scan all subdirectories in the folder. The SearchOption.AllDirectories enum is the best solution.Recursive File List

Note: EnumerateFiles is helpful on a recursive directory scan, as the result count might be large.

Here: We call EnumerateFiles to get all the deeply-nested files in directories. Notice the result files have different directories.

C# program that uses SearchOption.AllDirectories using System; using System.IO; class Program { static void Main() { // Recursively get file names for all files in a directory. // ... Use EnumerateFiles to accommodate large result count. foreach (string file in Directory.EnumerateFiles(@"c:\go\test\bench\", "*.*", SearchOption.AllDirectories)) { Console.WriteLine("IN DIRECTORY: " + file); } } } Output IN DIRECTORY: c:\go\test\bench\garbage\Makefile IN DIRECTORY: ... IN DIRECTORY: c:\go\test\bench\go1\binarytree_test.go IN DIRECTORY: ...
Performance, EnumerateFiles. Directory.GetFiles is fast when used on small directories with few files. But for large directories Directory.EnumerateFiles is faster.EnumerateFiles, GetFiles Benchmark
A summary. We saw the Directory.GetFiles method, which will return the list of files in a specified directory on the file system. We tested the method on the C:\ root directory.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to