
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to create a folder if it does not exist in C#?
For creating a directory, we must first import the System.IO namespace in C#. The namespace is a library that allows you to access static methods for creating, copying, moving, and deleting directories.
It is always recommended to check if the Directory exist before doing any file operation in C# because the complier will throw exception if the folder does not exist.
Example
using System; using System.IO; namespace DemoApplication { class Program { static void Main(string[] args) { string folderName = @"D:\Demo Folder"; // If directory does not exist, create it if (!Directory.Exists(folderName)) { Directory.CreateDirectory(folderName); } Console.ReadLine(); } } }
The above code will create a Demo Folder in the D: directory.
Directory.CreateDirectory can also be used to create subfolders.
Example
using System; using System.IO; namespace DemoApplication { class Program { static void Main(string[] args) { string folderName = @"D:\Demo Folder\Sub Folder"; // If directory does not exist, create it if (!Directory.Exists(folderName)) { Directory.CreateDirectory(folderName); } Console.ReadLine(); } } }
The above code will create a Demo Folder with a Sub Folder in the D: directory.
- Related Articles
- MySQL create user if it does not exist?
- How can I create a python directory if it does not exist?
- How can I create a directory if it does not exist using Python?
- Create view in MySQL only if it does not already exist?
- How to check if a table exists in MySQL and create if it does not already exist?
- Insert records in MongoDB collection if it does not exist?
- Create a table if it does not already exist and insert a record in the same query with MySQL
- Upsert in MongoDB while using custom _id values to insert a document if it does not exist?
- MongoDB query to determine if a specific value does not exist?
- Add object to array in JavaScript if name does not already exist?
- Does NOT EQUAL exist in MySQL?
- PHP and MYSQL database connection and table creation only once if it does not already exist?
- How to insert only those records that does not exist in a MySQL table?
- How to select from MySQL table A that does not exist in table B?
- Insert array where element does not exist else update it (with multiple conditions)?

Advertisements