How to Delete All Files in a Folder Except One in C#?

As a developer, there will be times when you required to delete all files in a folder except one in C#. In this C# tutorial, I will explain various methods to delete all files in a folder except one in C#.

Delete All Files in a Folder Except One in C#

To delete all files in a folder except one in C#, you can use one of the below 3 methods:

  • Method 1: Using Basic Looping
  • Method 2: Using LINQ
  • Method 3: DirectoryInfo Class

1. Delete All Files in a Folder Except One in C# using Basic Looping

The most straightforward way to delete all files in a folder except one in C# is by using a basic foreach loop to iterate through the directory and delete files conditionally.

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string folderPath = @"C:\ExampleFolder";
        string fileToKeep = "keepMe.txt";

        // Get all files in the directory
        string[] files = Directory.GetFiles(folderPath);

        // Loop through each file and delete it except the one to keep
        foreach (string file in files)
        {
            if (Path.GetFileName(file) != fileToKeep)
            {
                File.Delete(file);
                Console.WriteLine($"Deleted: {file}");
            }
        }
    }
}

The above C# code will delete all the files from the folder except the keepMe.txt file.

You can see here in the below screenshot, I have a folder having a few files inside it.

How to Delete All Files in a Folder Except One in C#

Then when I run the above code using a Console application using Visual Studio, you can see the output in the below screenshot.

Delete All Files in a Folder Except One in C#

2. Delete All Files in a Folder Except One in C# using LINQ

If you’re comfortable using LINQ (Language-Integrated Query), you can filter out the files you want to keep and then proceed to delete the remaining ones in C#.

Below is the complete code to delete all files in a folder except one in C# using LINQ.

using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        string folderPath = @"C:\ExampleFolder";
        string fileToKeep = "keepMe.txt";

        // Get all files and filter out the one to keep
        var filesToDelete = Directory.GetFiles(folderPath).Where(file => Path.GetFileName(file) != fileToKeep);

        // Delete the files
        foreach (var file in filesToDelete)
        {
            File.Delete(file);
            Console.WriteLine($"Deleted: {file}");
        }
    }
}

3. Delete All Files in a Folder Except One in C# using DirectoryInfo Class

Now, let us see how to delete all files in a folder except one in C# using the DirectoryInfo class.

Using the DirectoryInfo class, you can gather information about files in a directory and perform operations on them.

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string folderPath = @"C:\ExampleFolder";
        string fileToKeep = "keepMe.txt";

        DirectoryInfo dirInfo = new DirectoryInfo(folderPath);

        // Loop through each file and delete it except the one to keep
        foreach (FileInfo file in dirInfo.GetFiles())
        {
            if (file.Name != fileToKeep)
            {
                file.Delete();
                Console.WriteLine($"Deleted: {file.Name}");
            }
        }
    }
}

The above code will delete all the files from the folder except the keepMe.txt in C#.

Conclusion

In this tutorial, we have covered different methods to delete all files in a folder except one using C#. You can use basic C# looping, LINQ, or the DirectoryInfo class based on your preference and needs.

You may also like: