Reader Level:
ARTICLE

Delete a directory in C#

On July 17, 2012 Articles | Files, Directory, IO
How to delete a directory in C# and .NET.
  • 0
  • 0
  • 11358

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. 
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.  

string root = @"C:\Temp";      
// If directory does not exist, don't even try 
if (Directory.Exists(root))
{
    Directory.Delete(root);
}


Download free book: Working with Directories in C#

Article Extensions
Contents added by le Ha on Jul 08, 2013
good
Mahesh Chand

Mahesh Chand is founder of C# Corner. C# Corner founded in 1999 is a FREE member contributions based open platform for developers to solve problems, learn new technology and hang out.  M...

  • 1rank
  • 71M readers
  • 9times
COMMENT USING

Trending up