Home
C#
Directory.GetFiles Example (Get List of Files)
This page was last reviewed on Nov 15, 2023.
Dot Net Perls
Directory.GetFiles. This C# method returns the file names in a folder. It can be used with additional arguments for more power (like filtering).
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.
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.
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 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.
ToList
Convert List, Array
List
Tip 2 We specify an absolute path to 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.
Tip 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 ...
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.
File List Recursive
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; // 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.
Directory
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.
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 Nov 15, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.