Reader Level:
Articles

Check if a directory Exists in C#

By Mahesh Chand on July 17, 2012
How to Check if a directory Exists using C# and .NET.
  • 0
  • 0
  • 18595

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;

Check if a directory Exists 

The Directory.Exists method checks if the specified directory exists. The following code snippet checks if a directory exists or not and deletes only if the directory exists. 

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#

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.  Mahesh has been awar... Read more

COMMENT USING

Trending up