Create folder if not exist using C# DotNet

Create folder if not exist using C# DotNet

No comments

 233 total views,  6 views today

In this article, we will learn about how to create a folder if not exists using the C# dotnet coding. It is essential in any programing languages to know about it – when we work on a big enterprise project, many times we will be in such needs.

Using the below C# code we can create a folder or directory in the given path:

using System;
using System.IO;

namespace CreateFolderIfNotExists
{
class Program
{
static void Main(string[] args)
{
//Console.WriteLine("Hello World!");

string rootFolderPath = @"C:\Temp";
string folderDirectoryPathTobeCreated = @"C:\Temp\Test2";

Program folderCreationObject = new Program();
try
{
bool folderCreationStatus=folderCreationObject.CreateFolderIfNotExists(rootFolderPath, folderDirectoryPathTobeCreated);

if (folderCreationStatus == true)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Succesfully the new folder has been created in: " + folderDirectoryPathTobeCreated);
}
else if(folderCreationStatus == false)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("The folder is already exists in: " + folderDirectoryPathTobeCreated);
}

}
catch(Exception exp) //catch block catch the exception, if any found inside the try block
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(exp.Message.ToString());
}
finally //Finally block free the object memory.
{
Console.WriteLine("The code has been executed successfully.");
}

}

/// <summary>
/// The below function creates the folder or directory in the given path, if not exists which taskes
/// two parameters 1. Root Folder Path, 2. Sub Folder Path where the new folder should be created.
/// </summary>
/// <param name="rootPath">Root Folder Path</param>
/// <param name="folderTobeCreatedPath">Sub Folder Path</param>
public bool CreateFolderIfNotExists(string rootPath, string folderTobeCreatedPath)
{
bool isFolderCreated = false;

//If a root directory/folder does not exist, it will create the root folder.
if (!Directory.Exists(rootPath))
{
Directory.CreateDirectory(rootPath);
isFolderCreated = true;
}

//If a root directory/folder exists, it will create sub folder directory under the root folder.
if (Directory.Exists(rootPath))
{
if (!Directory.Exists(folderTobeCreatedPath))
{
Directory.CreateDirectory(folderTobeCreatedPath);
isFolderCreated = true;

}

}
return isFolderCreated;

}

}
}

Notes:

  • The folder and directory are the same.
  • In the above code, before creating the folder or directory first it checks whether it exists, if not then only create the folder in the given path.
  • The above code also explains, how to handle exception C# console coding and what is the uses of try, catch and finally block in C# dotnet.

Summary:

Thus, in this article, we have learned the below with respect to creating the folder or directory using the C# coding:

  • How to create the folder in the given path if not exists using C# coding.
  • How to create the directory in the given path if not exists using C# coding.
  • How to handle the exception in C# coding try, catch and finally block.
  • What is the use of the “Directory.CreateDirectory” method in C# coding.

 

See Also:

You may also like the below article on how to create a folder if not exists in PowerShell coding: