How to create, delete and check directory in C#.Net

In my previous article I have explained about How to Store/Retrieve dataset in and from viewstate In this article I am going to explain about how to create, delete and check whether the directory exists using C#.Net.

The System.IO.Directory class is used for directory manipulation. This class provides static methods for creating, copying, moving, and deleting directories and sub directories.To use this class we should import System.IO namespace first.

Create Directory:
The Directory.CreateDirectory method is used to creates a directory and sub directory in the specified path.
Delete Directory:
The Directory.Delete method is used to delete a directory. But If a directory has sub directories 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.
Check directory is Exists:
The Directory.Exists method is used to check whether the directory is exists in the specified path. It returns boolean value.
The below code checks whether rootFolder is exists in E drive. If it exists then it will delete that folder. Else it will create the folder.
string root = @”E:rootFolder”;
if (Directory.Exists(root))
Do you like this article? Help us to improve. Please post your comments below.

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!