How to delete all files from a directory

Asked

Viewed 1,315 times

5

I would like to know how to delete all files from a directory.

Suppose I need to delete all files from the Test folder in C: test.

  • No need to add [Resolvido] at the end of the post. Just accept a response that the system already marks its publication as "solved".

1 answer

6


In accordance with reply in English:

System.IO.DirectoryInfo di = new DirectoryInfo("YourPath");

foreach (FileInfo file in di.GetFiles())
{
    file.Delete(); 
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
    dir.Delete(true); 
}

or

System.IO.Directory.Delete(@"C:\Temp", true);

or

using System.IO;
using System.Linq;
var directory = Directory.GetParent(TestContext.TestDir);

directory.EnumerateFiles()
    .ForEachInEnumerable(f => f.Delete());

directory.EnumerateDirectories()
    .ForEachInEnumerable(d => d.Delete(true));    
  • 1

    It worked, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.