Do you need to delete a file from a folder in C#? In this C# tutorial, I will explain how to delete a file from a folder in C#. We will see various methods for deleting a file from a folder in C#. Also, I will show you how to delete all files from a folder in C#.
Delete a File from a Folder in C#
To work with files, you’ll first need to import the System.IO namespace. This namespace contains the File class, which provides static methods for file operations like deleting files from a folder in C#.
Delete a File Using File.Delete() method in C#
The simplest way to delete a file in C# is by using the File.Delete(string path) method. This method deletes the file specified by the given path.
Here’s a code snippet to delete a file in C#:
using System;
using System.IO;
class Program
{
static void Main()
{
string path = @"C:\Example\test.txt";
// Delete the file
File.Delete(path);
Console.WriteLine("File Deleted Successfully");
}
}
When performing file operations, it’s always a good practice to handle possible exceptions that could be thrown. The File.Delete() method can throw exceptions like UnauthorizedAccessException or FileNotFoundException.
Here’s how you can catch these exceptions:
using System;
using System.IO;
class Program
{
static void Main()
{
string path = @"C:\Example\test.txt";
try
{
// Delete the file
File.Delete(path);
Console.WriteLine("File Deleted Successfully");
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine($"Error: {e.Message}");
}
catch (FileNotFoundException e)
{
Console.WriteLine($"Error: {e.Message}");
}
}
}
When I run the in a Visual Studio console application, you can see the output in the screenshot below:
Here is the complete code:
using System;
using System.IO;
namespace DeleteFileExample
{
class Program
{
static void Main(string[] args)
{
string path = @"C:\Example\test.txt";
try
{
// Check if file exists
if (File.Exists(path))
{
// Delete the file
File.Delete(path);
Console.WriteLine("File Deleted Successfully");
}
else
{
Console.WriteLine("File does not exist.");
}
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine($"Error: {e.Message}");
}
catch (FileNotFoundException e)
{
Console.WriteLine($"Error: {e.Message}");
}
}
}
}
Delete All Files from a Folder in C#
Now, let us see how to delete all files from a folder in C#.
Delete All Files from a Folder Using Directory.GetFiles() in C#
We can delete all files from a folder using Directory.GetFiles() in C#.
To delete all files in a folder, you can first fetch the list of all files using the Directory.GetFiles(string path) method. This returns an array of strings containing the full paths of all files in the specified directory.
Once you have the list of files, you can iterate through it and use the File.Delete(string path) method to delete each file.
Here is how you do it:
using System;
using System.IO;
class Program
{
static void Main()
{
string folderPath = @"C:\ExampleFolder";
// Fetch all files
string[] files = Directory.GetFiles(folderPath);
// Loop through and delete each file
foreach (string file in files)
{
File.Delete(file);
}
Console.WriteLine("All Files Deleted Successfully");
}
}
File and folder operations can throw exceptions, such as UnauthorizedAccessException
or DirectoryNotFoundException
. It’s good practice to catch these exceptions and handle them gracefully.
Here’s an example of adding error handling:
using System;
using System.IO;
class Program
{
static void Main()
{
string folderPath = @"C:\ExampleFolder";
try
{
// Fetch all files
string[] files = Directory.GetFiles(folderPath);
// Loop through and delete each file
foreach (string file in files)
{
File.Delete(file);
}
Console.WriteLine("All Files Deleted Successfully");
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine($"Error: {e.Message}");
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine($"Error: {e.Message}");
}
}
}
Here is the complete code to delete all files from a folder in C#.
using System;
using System.IO;
namespace DeleteAllFiles
{
class Program
{
static void Main(string[] args)
{
string folderPath = @"C:\ExampleFolder";
try
{
// Check if folder exists
if (Directory.Exists(folderPath))
{
// Fetch all files
string[] files = Directory.GetFiles(folderPath);
// Loop through and delete each file
foreach (string file in files)
{
File.Delete(file);
}
Console.WriteLine("All Files Deleted Successfully");
}
else
{
Console.WriteLine("Folder does not exist.");
}
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine($"Error: {e.Message}");
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine($"Error: {e.Message}");
}
}
}
}
Conclusion
I hope now you know how to delete a file from a folder in C#; I have also explained how to delete all files from a folder in C# with an example.
You may like the following tutorials:
- C# File Naming Conventions
- How to Delete Oldest Files in a Directory Using C#?
- Folder Browser Dialog in C#
- How to Delete a Folder in C#?
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…