Solved

Delete all files in a folder - C#

Posted on 2009-03-30
8
6,138 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now