Delete A directory In C#

The System.IO.Directory class in the .NET Framework class library provides static methods for creating, copying, moving, and deleting directories and subdirectories. Before you can use the Directory class, you must import the System.IO namespace. 
  1. using System.IO;  
Delete a directory in C#
The Directory.Delete method deletes an empty directory from the specified path permanently. If a directory has subdirectories and/or files, you must delete them before you can delete a directory.  If you try to delete a file that is not empty, you will get an error message.
The following code snippet deletes the destination file.  
  1. string root = @"C:\Temp";        
  2. // If directory does not exist, don't even try   
  3. if (Directory.Exists(root))  
  4. {  
  5.     Directory.Delete(root);  
  6. }  
Download free book: Working with Directories in C#

Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.