Solved

c# delete all files in a directory

Posted on 2011-03-02
4
673 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
[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
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

Technology Partners: 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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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…

752 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