How to create a folder if not exist in C#.Net

In this C#.Net tutorial, we will discuss how to create a folder if not exist in C#.Net.

I got a requirement to create a folder if the folder does not exist using C#.Net in an asp.net web application.

Create a folder if not exist in C#.Net

C#.Net provides a call known as System.IO.Directory.CreateDirectory which we can use to check if a folder exists or not, as well as it will create the folder.

In C#.Net we can create a folder if it does not exists.

If you are working in a windows application, then the first thing is we need to add the System.IO dll reference (System.IO namespace).

You can add a button in the windows form application and write the below code in the button click event.

string folderPath = @"E:\Folder1";

try
        {
            if (Directory.Exists(folderPath))
            {
                //The code will execute if the folder exists
            }
            //The below code will create a folder if the folder is not exists in C#.Net.            
            DirectoryInfo folder = Directory.CreateDirectory(folderPath);          
        }
        catch (Exception e)
        {
            
        }
        finally
       {

       }

This is the code to create a folder in C#.Net if not exists.

You can also write the code like below:

string folderPath = @"E:\Folder1";

if (!Directory.Exists(folderPath))
    Directory.CreateDirectory(folderPath);

The above code will create a folder if the folder not exists in C#.Net.

You can also write the code in just one line like below:

string folderPath = @"E:\Folder1";

Directory.CreateDirectory(folderPath);

It will not do anything if the folder or directory exists, else it will create the directory if not exists in C#.Net.

How to create a subfolder (directory) in C#.Net

In the same way, in C#.Net we can create a subfolder or a directory using the CreateDirectory method of System.IO namespace.

Below is the code you can use to create a sub folder in C#.Net.

string subFolderPath = @"E:\Folder1\Folder1-1";

if (!Directory.Exists(subFolderPath))
{
    Directory.CreateDirectory(subFolderPath);
}

The above code will check if the subfolder exists or not, else it will create the subfolder in C#.Net.

Delete a folder if exists in C#.Net

By using the below code, we can delete a folder if it exists in the local system. It will first check if the folder or directory exists or not. If it exists, then it will delete the folder in C#.Net.

string folderPath = @"E:\Folder1";

if (Directory.Exists(folderPath))  
{  
    Directory.Delete(folderPath);  
}

Create a folder in Asp.Net using C#.Net

Now, we will see how to create a folder in asp.net using C#.Net, in this case, the folder will be created in the server rather in the local system. If you will run locally then it will create the folder in the file system.

Here, we have to use Server.MapPath to check the folder path.

string folderPath = Server.MapPath("~/Images/");

if (!Directory.Exists(folderPath))
    {
	Directory.CreateDirectory(folderPath);
    }

How to check if file exists in C#.Net

We can easily check if the file exists or not in C#.Net. Here File.Exists method will return true if the file exists else it will return false.

string filePath = @"E:\Folder1\filename.docx";

if (File.Exists(filePath))
{
//This is called if file exist in C#.Net.
}
else
{
//This will called if file does not exist in C#.Net.
}
How to create a folder if not exist in C#.Net
How to create a folder if not exist in C#.Net

You may like the following C#.Net tutorials:

In this C#.Net tutorial, we learned the below things:

  • Create a folder if not exist in C#.Net
  • How to create a subfolder (directory) in C#.Net
  • Delete a folder if exists in C#.Net
  • Create a folder in Asp.Net using C#.Net
  • How to check if file exists in C#.Net