One of the most common developer operations is file handling. Whether we are reading or writing the data to a file, you are bound to carry out the file operations in your development tasks.
In C#, the directory is one of the most fundamental methods that can facilitate the file-handling “operations.GetFiles” method. This method is part of the System.IO namespace and is crucial in retrieving the files from a directory.
In this tutorial, we will understand how to use the “Directory.GetFiles” method in C# and cover the practical examples of using it.
The Basics
As you can guess, before using the “GetFiles” method, we must import the System.IO namespace in our C# code.
We can do this with a basic “using” directive as follows:
Once we import the namespace, we can access the “Directory” class where the “GetFiles” method resides.
The function syntax and definition are as follows:
The function accepts a string that represents a relative or absolute path of the directory whose files you wish to show. The provided parameter is not case-sensitive.
The function then returns an array of strings that contains the full names which includes the paths for the files in the specified directory. The function returns an empty array if there are no files in the set directory.
We can simplify the function usage as follows:
Example 1: Basic Usage
The previous example code demonstrates how to use the “GetFiles” function to get a list of all the files in a specified directory.
using System.IO;
class Program
{
static void Main()
{
string[] files = Directory.GetFiles(@"./");
foreach (var file in files)
{
Console.WriteLine(file);
}
}
}
In this example code, we use the GetFiles() method to get a list of the files in the current working directory.
We then iterate over each item in the array to print each file in the directory. The resulting output is as follows:
./Files.dll
./Files.exe
./Files.pdb
./Files.runtimeconfig.json
Example 2: Filters
The “GetFiles” method allows us to specify an overload which we can use to set a specific file pattern. This only returns the matching files as demonstrated in the following example:
using System.IO;
class Program
{
static void Main()
{
string[] txtFiles = Directory.GetFiles(@"./", "*.exe");
foreach (var file in txtFiles)
{
Console.WriteLine(file);
}
}
}
In this case, the given program should only return the files that end in “.exe” extension as demonstrated in the following output:
Example 3: Error Handling
Like all good programs, it is essential to implement the error-handling mechanisms, especially when dealing with file handles. For example, you can handle the errors such as a nonexisting directory, read permission error, etc.
The following example code creates a basic error-handling mechanism if the specified directory does not exist:
using System.IO;
class Program
{
static void Main()
{
string path = @"C:\hello";
if (Directory.Exists(path))
{
string[] files = Directory.GetFiles(path);
foreach (var file in files)
{
Console.WriteLine(file);
}
}
else
{
Console.WriteLine($"The directory specified does not exist.");
}
}
}
In this case, the program first tests whether the specified directory exists within the filesystem. If it exists, the function retrieves the files inside it. Otherwise, return an error that indicates that the directory does not exist.
The directory specified does not exist.
Example 4: Recursive File Fetch
Sometimes, you might wish for the function to get inside the nested directories and fetch the files from those directories. This is also known as a recursive file fetch.
We can take advantage of the SearchOption enumeration which specifies whether the search operation should include all subdirectories or only the current directory.
Consider the following example source code to see the usage:
using System.IO;
class Program
{
static void Main()
{
string[] allFiles = Directory.GetFiles(@"../../", "*.*", SearchOption.AllDirectories);
foreach (var file in allFiles)
{
Console.WriteLine(file);
}
}
}
In this case, we provide the “SearchOption.AllDirectories” option tells the search operation that we wish to include the subdirectories in the search.
An example output is as follows:
../../Debug\net7.0\Files.dll
../../Debug\net7.0\Files.exe
../../Debug\net7.0\Files.pdb
../../Debug\net7.0\Files.runtimeconfig.json
Conclusion
In this tutorial, we walked you through all the features and functionality of the “GetFiles” method to retrieve the files from a given directory. We also covered how you can perform the filtering and recursive fetch if necessary.