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

Delete File or Folder path in C#

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

Was this helpful?
YesNo
Updated on June 2021