Directory.GetFiles Example (Get List of Files)Use the Directory.GetFiles and EnumerateFiles methods from System.IO.
This page was last reviewed on Aug 5, 2021.
Directory.GetFiles. This C# method returns the file names in a folder. It can be used with additional arguments for more power (like filtering).
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.
Detail 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.
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); } } }
--- 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.
Convert List, Array
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); } }
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.
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); } } }
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.
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); } } }
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.
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 is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Aug 5, 2021 (image).
© 2007-2023 Sam Allen.