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
exampleYou 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.
GetFiles
. The "programs" path directory is located within the home directory, and all paths within are returned.GetFiles
with 2 arguments. The second argument uses the pattern "*.TXT" to filter the paths returned.Directory.GetFiles
, it is relative to the home directory, unless it is an absolute path.using System; using System.IO; // Step 1: put all file names in directory into array. string[] array1 = Directory.GetFiles("programs/"); Console.WriteLine("--- Files: ---"); foreach (string name in array1) { Console.WriteLine(name); } // Step 2: put all bin files in directory into array. // ... This is case-insensitive. string[] array2 = Directory.GetFiles("programs/", "*.TXT"); Console.WriteLine("--- TXT Files: ---"); foreach (string name in array2) { Console.WriteLine(name); }--- Files: --- programs/spellbug ... --- TXT Files: --- programs/search-perls.txt programs/example.txt programs/words.txt
List
exampleYou 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.
System.Linq
namespace at the top first—it is included by default in new files.GetFiles
in this example—this means it is not relative to the home directory.using System; using System.Collections.Generic; using System.IO; using System.Linq; string[] array1 = Directory.GetFiles("/Users/Sam/programs/"); // Get list of files. List<string> filesList = array1.ToList(); Console.WriteLine(filesList.Count);21
EnumerateFiles
With EnumerateFiles
, each file name is returned in an IEnumerable
collection—so the entire string
array of file names can be avoided.
EnumerateFiles
can be faster for large result lists. But for small directories, GetFiles
may be faster.using System; using System.IO; // Loop over all files in programs directory. foreach (string path in Directory.EnumerateFiles("programs/")) { Console.WriteLine("IN PROGRAMS DIRECTORY: " + path); }IN PROGRAMS DIRECTORY: programs/spellbug ...
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.
EnumerateFiles
is helpful on a recursive directory scan, as the result count might be large.EnumerateFiles
to get all the deeply-nested files in directories. Notice the result files have different directories.using System; using System.IO; // Recursively get file names for all files in a directory. // ... Use EnumerateFiles to accommodate large result count. foreach (string file in Directory.EnumerateFiles("programs/", "*.*", SearchOption.AllDirectories)) { Console.WriteLine("IN DIRECTORY: " + file); }IN DIRECTORY: programs/rewrite.go ... IN DIRECTORY: programs/vbtest/Program.vb
EnumerateFiles
Directory.GetFiles
is fast when used on small directories with few files. But for large directories Directory.EnumerateFiles
is faster.
The Directory.GetFiles
method will return the list of files in a specified directory on the file system. When we use a relative path, it is relative to the home directory.