Create and Delete Directory in ASP.NET

In this article, we will tell you how to create and delete directories in ASP.NET.
  
To create a directory please use the procedure given below:
 
1. Used the namespace:
  1. using System.IO;  
2. Get the path where you want to create the directory like:
  1. string path = Server.MapPath(Path where you want to create the directory);  
3. Check if the directory already exists on the given path; if it does not exist then create the directory on the given path, as in:
  1. //Checking directory already exist or not at the given path  
  2. if (!Directory.Exists(path))  
  3. {  
  4.     //Create the directory on the given path  
  5.     Directory.CreateDirectory(path);  
  6.     Response.Write("Directory has been created successfuly.");  
  7. } 
To delete the directory from the given path please use the procedure given below.
 
1. Check if the directory already exists on the given path; if it already exists then delete that directory from the given path using the following:
  1. if (Directory.Exists(path))  
  2. {  
  3.     // Delete the directory from the given path  
  4.     Directory.Delete(path);  
  5.     Response.Write("Directory has been deleted successfuly.");  
  6. }  
This is a simple live example of how to create and delete a directory in ASP.Net. In this example, I am creating the directory inside the "DirectoryList" folder.
 
CreateDirectory1.png

In aspx.cs:
 
CreateDirectory2.png
Result:

CreateDirectory3.png


Similar Articles