Solved

c# delete all files in a directory

Posted on 2011-03-02
4
670 Views
Last Modified: 2013-11-10
Seeing if someone has a function that takes a directory path as a variable.
Then code deletes all files in that particular directory.  
0
Comment
Question by:carsRST
  • 2
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
gdupadhyay earned 500 total points
ID: 35021948
You can do by following any one below:
using System.IO;

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

or

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

Open in new window

0
 
LVL 9

Expert Comment

by:gdupadhyay
ID: 35021960
0
 
LVL 16

Author Comment

by:carsRST
ID: 35022069
Thanks - I'll give it a try...
0
 
LVL 16

Author Closing Comment

by:carsRST
ID: 35022134
Bless your heart.

Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

679 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