.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> File Operation >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Code to delete all the files present in a directory

Posted By:Maverick       Posted Date: July 26, 2011    Points: 40    Category: File Operation    URL: http://www.dotnetspark.com  

Code to delete all the files present in a directory. The code posted below will delete all the files in the directory specified.
 

Hello All,

The code posted below will delete all the files in the directory specified.

 private void DeleteAllFiles(string directoryPath)
    {
        if (Directory.Exists(HttpContext.Current.Server.MapPath(directoryPath)))
        {
            string[] filenames = System.IO.Directory.GetFiles(HttpContext.Current.Server.MapPath(directoryPath));
            foreach (string filename in filenames)
            {
                File.Delete(filename);
            }
        }
    }

Here the directory path is the path of directory in which all the files are present.

Hope it helps

Thanks,
Maverick

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend