Solved

Delete all files in a folder - C#

Posted on 2009-03-30
8
6,137 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
optimize  c# code 7 48
Data Saving(2) 2 26
Long URL in SMS 6 24
Achieve json result 2 32
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

14 Experts available now in Live!

Get 1:1 Help Now