Get All File Names in a Directory in C#

  1. Get All File Names in a Directory With the Directory.GetFiles() Method in C
  2. Get All File Names in a Directory With the DirectoryInfo.GetFiles() Method in C

This tutorial will discuss the methods of getting the file names of all the files inside a specific directory in C#.

Get All File Names in a Directory With the Directory.GetFiles() Method in C

The Directory.GetFiles() method in C# gets the names of all the files inside a specific directory. The Directory.GetFiles() method returns an array of strings that contains the absolute paths of all the files inside the directory specified in the method parameters. See the following example.

using System;
using System.IO;

namespace get_all_files
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] files = Directory.GetFiles(@"C:\File", "*.txt");
            foreach (var file in files)
            {
                Console.WriteLine(file);
            }
        }
    }
}

Output:

C:\File\file.txt
C:\File\file1.txt

In the above code, we extracted the names of all the files with a .txt extension inside the directory C:\File with the Directory.GetFiles() method in C#.

The Directory.GetFiles() method can be used for various purposes. For example, the following code example gives us all the files’ names regardless of their file extension in C#.

using System;
using System.IO;

namespace get_all_files
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] files = Directory.GetFiles(@"C:\File");
            foreach (var file in files)
            {
                Console.WriteLine(file);
            }
        }
    }
}

Output:

C:\File\file.txt
C:\File\file1.txt
C:\File\readme.md

We can also get the file names of all the files included in any subdirectories with the Directory.GetFiles() method. See the following example.

using System;
using System.IO;

namespace get_all_files
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] files = Directory.GetFiles(@"C:\File", "*.txt", SearchOption.AllDirectories);
            foreach (var file in files)
            {
                Console.WriteLine(file);
            }
        }
    }
}

Output:

C:\File\file.txt
C:\File\file1.txt
C:\File\subFile\file3.txt
C:\File\subFile\file4.txt

In the above code, we extracted the file names of all the files with the .txt extension in all the sub-directories of the C:\File directory with the Directory.GetFiles() method in C#.

Get All File Names in a Directory With the DirectoryInfo.GetFiles() Method in C

The DirectoryInfo.GetFiles() method gets the files inside a specified directory in C#. The DirectoryInfo.GetFiles() method returns an array of the FileInfo class objects that contain information about all the files in the directory specified inside the method parameters. We can get the names of the files with the FileInfo.Name property. The following code example shows us how we can get the files’ file names inside a specified directory with the DirectoryInfo.GetFiles() method in C#.

using System;
using System.IO;

namespace get_all_files
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo di = new DirectoryInfo(@"C:\File");
            FileInfo[] files = di.GetFiles("*.txt");
            string str = "";
            foreach (FileInfo file in files)
            {
                Console.WriteLine(file.Name);
            }
        }
    }
}

Output:

file.txt
file1.txt

In the above code, we extracted the file names of all the files with .txt extension inside the C:\File directory with the DirectoryInfo.GetFiles() method in C#. We can also use the SearchOption.AllDirectories parameter inside the DirectoryInfo.GetFiles() method to get the names of all the files in all the sub-directories. Meanwhile, we can also get all the files’ names regardless of their file extensions by not specifying any file extensions in the DirectoryInfo.GetFiles() method. The only difference between the previous approach and this approach is that the Directory.GetFiles() method gives us the complete absolute paths of the files. In contrast, the DirectoryInfo.GetFiles() method gives us only all the files’ names.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp File

  • Delete a File in C#
  • Get Relative Path of a File in C#