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.
Step 1 We call GetFiles. The "programs" path directory is located within the home directory, and all paths within are returned.
Step 2 We call GetFiles with 2 arguments. The second argument uses the pattern "*.TXT" to filter the paths returned.
Important For the path argument to Directory.GetFiles, it is relative to the home directory, unless it is an absolute path.
// Step 1: put all file names in directory into array.
string array1 = Directory.GetFiles("programs/");
Console.WriteLine("--- Files: ---");
foreach (string name in array1)
// 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)
}--- Files: ---
--- TXT Files: ---
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.
Tip 2 We specify an absolute path to GetFiles in this example—this means it is not relative to the home directory.
string array1 = Directory.GetFiles("/Users/Sam/programs/");
// Get list of files.
List<string> filesList = array1.ToList();
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.
// 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
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.
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.
// 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
Performance, EnumerateFiles. Directory.GetFiles is fast when used on small directories with few files. But for large directories Directory.EnumerateFiles is faster.