If a folder does not exist create it using C#

In C#, you can programmatically create a folder if it doesn't already exist using simple code snippets.

Steps to Create a Folder:

  • Check if the folder exists: Use Directory.Exists() method to check if the folder exists.
  • Create the folder: If the folder doesn't exist, use Directory.CreateDirectory() method to create it.

Code Example:

using System.IO;
string folderPath = @"C:\path\to\your\folder";

if (!Directory.Exists(folderPath))
{
    Directory.CreateDirectory(folderPath);
    Console.WriteLine("Folder created successfully.");
}
 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice