Showing posts with label GetFiles. Show all posts
Showing posts with label GetFiles. Show all posts

Wednesday, November 02, 2011

Delete All Files using C#

For Deleting all the files first you need to get the list of file names from the specified directory (using static method Directory.Get­Files. Then delete all files from the list.
Method 1
using System.IO;

string[] filePaths = Directory.GetFiles(@"c:\Directory\");
foreach (string filePath in filePaths)
  File.Delete(filePath)

Method 2

To Delete all files using one code line, you can use Array.ForEach with combination of anonymous method.

Array.ForEach(Directory.GetFiles(@"c:\MyDirectory\"),delegate(string path) 
{ File.Delete(path); });