C#, how to delete all files and folders in a directory?

Posted by JL on Stack Overflow See other posts from Stack Overflow or by JL
Published on 2009-08-17T15:48:19Z Indexed on 2010/05/04 15:38 UTC
Read the original article Hit count: 195

Filed under:
|

Using C#, how can I delete all files and folders from a directory, but still keep the root directory?

I have this

  System.IO.DirectoryInfo downloadedMessageInfo = new DirectoryInfo(GetMessageDownloadFolderPath());

        foreach (FileInfo file in downloadedMessageInfo.GetFiles())
        {
            file.Delete(); 
        }
        foreach (DirectoryInfo dir in downloadedMessageInfo.GetDirectories())
        {
            dir.Delete(true); 
        }

Is this the cleanest way to do it?

© Stack Overflow or respective owner

Related posts about c#

Related posts about system.io