How to Delete File in a Path in .Net Core Using C#.Net

How to delete a specific file in a given folder path in .NET core console or windows application using C#.Net by using File.Delete("") method.
 In today's article I will show you how you can delete a specific file available in a folder in your .NET core application using c#.net. I have already explained how you can delete a folder in your .NET core application using c#.net. This code example you can use in your .NET core console application and in windows application. 

So, for this article first we will create a folder and add some files in it as shown below. 

Files in a Path in ..Net Core Using C#.Net
We will user .NET core console application to demonstrate how we can delete the file available in a folder. Here we are having the complete the code. 
string folderPath = "D:\\DemoRepo\\Myfolder";
string fileName = "no-profile-image.jpg";
if (Path.Exists(folderPath))
{
    string filePath = string.Format("{0}/{1}", folderPath, fileName);
    if (File.Exists(filePath))
    {
        try
        {
            File.Delete(filePath);
            Console.WriteLine("File deleted successfully....");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
    }
    else
    {
        Console.WriteLine("Sorry file not found....");
    }
}
Let's discuss the above code. Here we have taken two string variables first one is folderPath and second one is fileName.  After that I have used Path.Exists("folder Path") to validate the folder path if path exists after that the I have used string.Format to prepare the file path by concatenating or combining string path of folder and file name to get complete file path. 

After getting the full path i have used File.Exists("file Path") to validate file exists in the given path or not. If file does not exist, we will display a friendly message that file does not exist and exit. Now if file exists, I have used File.Delete("file path") to delete the file. 

Now we have done let's run the code and check the output. 

Delete file in .NET core using c#.net

Now let's check the folder which i having the file. file will be deleted from the folder. 

Folder With Files
Now let's discuss the exception and the problems which user faces while deleting a file. 

Let's first check the scenario where the file dies not exists and we are trying the delete the file. We will not receive any error or exception while deleting the file File.Delete(filePath); will execute success fully. But it is always good practice to validate the file existence by using File.Exists(filePath) and display a user-friendly message to user. 

Now let's keep the file in open state and try to delete the file. From above file list lets open the textfile 1.txt and tr to delete it. We will receive the below exception.
Error! The process cannot access the file 'D:\DemoRepo\Myfolder\documentfile.docx' because it is being used by another process.
So, to avoid the above exception first we need to close the file or kill the process and then try to execute the code to delete the file. Always user try catch block the handle any kind of exceptions.

Post a Comment