1. Home
  2. Delete File or Folder path in C#

Delete File or Folder path in C#

KB ID: 42G2101204
Views: 100
Updated: June 2021

Following is a C# code snippet to delete a path (file or directory). First, we have to determine whether the path is a file or a folder and then call appropriate Delete methods. In case of a directory, you can specify whether the contents should be recursively deleted before deleting the directory.

void DeletePath(string path)
{
try
{
FileInfo finfo = new FileInfo(path);
if (finfo.Attributes == FileAttributes.Directory)
{
//recursively delete directory
Directory.Delete(path, true);
}
else if(finfo.Attributes == FileAttributes.Normal)
{
File.Delete(path);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}

For more details on our products, click here 
If you need further assistance, please submit a ticket here