In this blog we are going to see how to delete all the files and sub directories from the specified path.

Used Namespaces:

using System.IO;

Usage:

DeleteAllFiles(new DirectoryInfo("path"));


Used Helper method:

//Delete all the files from the path.

void DeleteAllFiles(DirectoryInfo diPath)

{

//Gets the files from the specific directory

foreach (FileInfo fiCurrFile in diPath.GetFiles())

{

fiCurrFile.Delete();

}

//Getting Sub directories

foreach (DirectoryInfo diSubFolder in diPath.GetDirectories())

{

DeleteAllFiles(diSubFolder); // Call recursively for all subfolders

}

}

Thanks for reading this article. Have a nice day.