Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c# delete all files in a directory

Posted on 2011-03-02
4
Medium Priority
?
682 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 2000 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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

670 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