C# Program to Get all Files Present in a Directory


Introduction

On the computer, we can store files in a directory, also known as a folder. A directory also contains shortcuts to other directories and files. If we want to know what all files have stored in a directory then C# also offers an easy way to do so. In this article, we will be learning a C# program to get all files present in a directory.

There are more than a couple of ways to know the files available in the directory. Let us discuss them in the upcoming sections.

1. GetFiles() Method

For knowing the name of the files that are present in the specified directory we use the GetFiles() method.

public static string[] GetFiles (string path); 

We can use GetFiles() and GetDirectories() to know the files and subdirectories in the specified directory.

Its parameter i.e., the absolute or relative path of the directory is a string. And it is caseinsensitive. This function returns an array that contains the list of names of files in the specified directory along with their paths. An empty array is also returned at times when the directory is blank.

Algorithm

Now, let’s discuss the algorithm to get all the files present in a directory using GetFiles() method.

Step 1 − Firstly, we declare a string to store the address of the directory.

Step 2  We get the list of the files by using GetFiles() and store it in an array named fyles.

Step 3  Finally, we print the list of files.

Example

using System;
using System.IO;
public class Example {
   public static void Main() {
      string directloc = @"D:\mypc\addre";

      // files list from the root directory and prints it
      string[] fyles = Directory.GetFiles(directloc);
      Console.WriteLine(String.Join(Environment.NewLine, fyles));
   }
}

Output

abrew.txt
zuma.txt

Now, to get the details of the types of files that are present in the root directory i.e., the directory we are searching and its subfolders then we use the ‘*’ pattern and SearchOption.AllDirectories which retrieve the multiple types of files available in the directory and its subdirectories.

Algorithm for SearchOption.AllDirectories

Now, let’s discuss the algorithm to get all the files present in a directory and its subdirectories using SearchOption.AllDirectories method.

Step 1  Firstly, we declare a string to store the address of the directory.

Step 2  We get the list of the files in the directory and its subdirectories by using SearchOption.AllDirectories and store it in an array named fyles.

Step 3  Finally, we print the list of files.

Example

using System;
using System.IO;

public class Example {
   public static void Main() {
      string directloc = @"D:\mypc\addre";

      // files list from the root directory and its subdirectories and prints it
      string[] fyles = Directory.GetFiles(directloc, "*", SearchOption.AllDirectories);
      Console.WriteLine(String.Join(Environment.NewLine, fyles));

   }
} 

Output

abrew.txt
zuma.txt

So, in this way we can get to know about the files in a directory and its subdirectories by using GetFiles() method. Now, moving to the next section we discuss the EnumerateFiles method to know about the files in a directory and its subdirectories.

2. EnumerateFiles Method

From the method's name, we can say that this is an enumerable collection returning method. So, this method returns an enumerable collection of complete file names in a given directory that matches the user-defined search and additionally explores folders.

public static System.Collections.Generic.IEnumerable EnumerateFiles (string path, string searchPattern, System.IO.SearchOption searchOption); 

Here, searchOption is the parameter that tells whether the search should include only the present path or all subdirectories. searchPattern is the search string that matches the names of the files in the user-defined path. It can have a valid literal path and wildcard(* and ?) characters, but not regular expressions.

Algorithm

Now, let’s discuss the algorithm to get all the files present in a directory using Directory.EnumerateFiles() method.

Step 1  Firstly, we declare a string to store the address of the directory.

Step 2  We get the list of the files in the directory and its subdirectories by using Directory.EnumerateFiles(directloc, "*", SearchOption.AllDirectories); and store it in a variable named fyles.

Step 3  Finally, we print the list of files.

Example

using System;
using System.IO;
using System.Collections.Generic;

public class Example {
   public static void Main() {
      string directloc = @"D:\mypc\addre";

      // files list from the root directory and its subdirectories and prints it
      var fyles = Directory.EnumerateFiles(directloc, "*", SearchOption.AllDirectories);
      Console.WriteLine(String.Join(Environment.NewLine, fyles)); 
   }
} 

Output

abrew.txt
zuma.txt

The searchPattern is very important in this method as it is a mix of the wildcard and literal characters. It does not allow regular expressions. Following are the wildcard specifiers and their matches.

Wildcard Specifier

Matches

*(asterisk)

Zero or more characters in that position

?(question mark)

Exactly one character in that position

If we use '*o' then each file name is checked to end with o. And if we use 'a*' then each file name is checked to start with a. Also when the asterisk wildcard character is used in searchPattern and the name of a three-character file extension, such as "*.txt," this returns files with extensions that have with the stated extension. Now, let’s see another method.

3. Directory.GetFileSystemEntries() Method

This method returns the names of all files and subdirectories that meet the conditions given by the programmer. The syntax for this method is as follows.

public static string[] GetFileSystemEntries (string path); 

Another option is to make advantage of the Directory. GetFileSystemEntries() retrieves the names of all files and subdirectories in the path supplied. It is overburdened with search patterns and search choices. When a search pattern is supplied, the method compares it to the names of files and folders in the path. If the SearchOption.AllDirectories option is used, it will search through all subdirectories.

Algorithm

Now, let’s discuss the algorithm to get all the files present in a directory using Directory.GetFileSystemEntries() method.

Step 1  Firstly, we declare a string to store the address of the directory.

Step 2 − We get the list of the files in the directory and its subdirectories by using Directory.GetFileSystemEntries(rootdir, "*", SearchOption.AllDirectories); and store it in an array.

Step 3  Finally, we print the list of files.

Example

using System;
using System.IO;

public class Example {
   public static void Main() {
      string directloc = @"D:\mypc\addre";
      
      // files list from the root directory and its subdirectories and prints it
      string[] fyles = Directory.GetFileSystemEntries(directloc, "*", SearchOption.AllDirectories);
      Console.WriteLine(String.Join(Environment.NewLine, fyles));
   }
} 

Output

abrew.txt
zuma.txt

Conclusion

So, with this comes the end of the article. In this article, we have learned about how to write a C# program to get all files present in a directory. We discussed the different methods to do so. We also understood the algorithms of the methods and learned their codes. We hope that this article enhances your knowledge regarding C#.

Updated on: 21-Apr-2023

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements