I want to delete all the files in a folder, and delete all the files in all its sub folders, and sub sub folders, etc, BUT I do not want to delete the folders themselves.
What would be the simplest way to do this?
I want to delete all the files in a folder, and delete all the files in all its sub folders, and sub sub folders, etc, BUT I do not want to delete the folders themselves.
What would be the simplest way to do this?
foreach (var file in Directory.EnumerateFiles("path", "*", System.IO.SearchOption.AllDirectories))
{
//TODO consider error handling
File.Delete(file);
}
static void DirSearch(string sDir)
{
try
{
foreach (string d in Directory.GetDirectories(sDir))
{
foreach (string f in Directory.GetFiles(d))
{
//Delete files, but not directories
File.Delete(f);
}
//Recursively call this method, so that each directory
//in the structure is wiped
DirSearch(d);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}