Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Delete all files in a folder - C#

Posted on 2009-03-30
8
Medium Priority
?
6,186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 30

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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

604 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