0

How do I delete the files in a folder in a directory via a button click?

This is the currant method I have tried.

    protected void BtnDel_Click(object sender, EventArgs e)
{
    Array.ForEach(Directory.GetFiles(Server.MapPath("/Notes/"), File.Delete)
}

This doesn't seem to be working so any alternative would be appreciated

Card
  • 23
  • 3
  • 3
    Please read [ask]. "Doesn't seem to be working" is not a proper problem description. – CodeCaster Apr 01 '16 at 19:13
  • Please take a look on [this topic](http://stackoverflow.com/questions/1288718/how-to-delete-all-files-and-folders-in-a-directory)! You can do it like various methods. – dori claudino Apr 01 '16 at 19:17

3 Answers3

4

This question should provide you with plenty of information.

Code snippet from there:

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

foreach (FileInfo file in di.GetFiles())
{
    file.Delete(); 
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
    dir.Delete(true); 
}
Community
  • 1
  • 1
Kiel
  • 408
  • 4
  • 13
2

To delete files in a folder use this code (can put it inside your event handler):

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

foreach (FileInfo file in di.GetFiles())
{
    file.Delete(); 
}
Bad
  • 4,967
  • 4
  • 34
  • 50
1

Try this:

System.IO.DirectoryInfo info = new DirectoryInfo(Server.MapPath("/Notes/");

foreach (var file in info.GetFiles())
 file.Delete(); 
Rono
  • 3,171
  • 2
  • 33
  • 58