You want to list all the files in a folder or directory using the .NET Framework's methods in the System.IO namespace and the C# programming language. The framework provides powerful methods on the Directory class that can help you list files on the filesystem. Here we look at the Directory.GetFiles method, which allows you to get a string array of the file names, using the C# language.
First, 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. The example program here uses the C:\ directory and lists all the files in that folder. Additionally, it filters the files in that directory and only displays the ones with the "BIN" extension.
=== Program that gets files in directories (C#) ===
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 txt files in root directory into array.
string[] array2 = Directory.GetFiles(@"C:\", "*.BIN"); // <-- Case-insensitive
// 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 of the program ===
--- Files: ---
(All files in your C:\ folder)
--- BIN Files: ---
(All .bin, .BIN files in your C:\ folder)Directory.GetFiles method. This method is a static method, meaning you do not need to create a Directory instance to use it. The first parameter to the method is the directory path. In a Windows Forms program, you can use Environment.SpecialFolder or the FolderBrowerDialog form. In ASP.NET, you can use Server.MapPath or Request.PhysicalApplicationPath.
Case-sensitivity. The second invocation to the Directory.GetFiles method has two parameters in its argument list. The second parameter uses the pattern "*.BIN". You have seen these patterns in the "Open File..." dialogs in Windows before. The pattern string is case-insensitive, meaning it will match files ending in "BIN", "Bin" and "bin". Finally, the program displays the file paths received.
Here we note how you can get a List collection of the file paths in a directory. To get the List, you will use the same method calls as in the code example in this article. However, you can convert the arrays to Lists with the ToList() extension method. Include the System.Linq namespace at the top first—it is included by default in new files.
(See ToList Extension Method.)
Often you will need to get the list of files in a certain directory, and then scan all subdirectories in the folder. There are several ways to accomplish this task, including some methods that use true recursion, and methods that use the Stack generic collection. The author recommends the method that uses Stack.
(See Recursive File and Directory Method.)
Here we saw the Directory.GetFiles method, which will return the list of files in a specified directory on your hard drive, using the C# language. We tested the method on the C:\ root directory, and discussed issues related to its usage. Finally, we noted ways to get a List collection of the array and ways to get subdirectories as well as the first directory.