I want to delete the entire folder or directory along with files and folders contained in it. how can I implement in c#?

Recommended Answers

All 2 Replies

Do it like:

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

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

or:

public void DeletingFilesAndFolders(DirectoryInfo directory)
{
    foreach(System.IO.FileInfo file in directory.GetFiles()) file.Delete();
    foreach(System.IO.DirectoryInfo subDirectory in directory.GetDirectories()) subDirectory.Delete(true);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.