Solved

c# delete all files in a directory

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

914 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

18 Experts available now in Live!

Get 1:1 Help Now