Solved

Delete all files in a folder - C#

Posted on 2009-03-30
8
6,142 Views
Last Modified: 2012-05-06
How can you delete all of the files in a directory with ASP.Net/C#?
0
Comment
Question by:dodgerfan
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 9

Expert Comment

by:cdaly33
ID: 24022983

    Dim files As String() = IO.Directory.GetFiles("path with files to delete")
    For Each file In files
      IO.File.Delete(file)
    Next

Open in new window

0
 
LVL 9

Accepted Solution

by:
cdaly33 earned 250 total points
ID: 24022993
Now in c# as requested.
string[] files = IO.Directory.GetFiles("path with files to delete");
    foreach (var file in files) {
        IO.File.Delete(file);
    }

Open in new window

0
 
LVL 27

Expert Comment

by:nmarun
ID: 24022996

// Delete files on the server:
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Data"));
FileInfo[] files = di.GetFiles();
foreach (FileInfo file in files)
{
    file.Delete();
}
 
// Delete files on the client's machine
// will lead to a serious security breach
// NO GO

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 27

Expert Comment

by:nmarun
ID: 24023014
cdaly33: your code will cause issues:
string[] files = IO.Directory.GetFiles("path with files to delete");

Gives the error:
Cannot convert source type System.IO.FileInfo[ ] to target type string[ ]

0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 24023187
As I understand user needs to have rights to delete. Just in case.

See also other solution:

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23630424.html
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24024623
The folder with the files in it needs to have Full Permission granted to NETWORK_SERVICE

Danger Will Robinson...
0
 
LVL 3

Expert Comment

by:coagantus
ID: 24026104
string[] files = Directory.GetFiles(@"C:\temp");
foreach (string file in files)
{
    File.Delete(file);
}
0
 
LVL 3

Assisted Solution

by:coagantus
coagantus earned 250 total points
ID: 24026116
Delete all files:

using System.IO;

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

OR

Delete all files (one-row example):

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

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question