Open In App

C# Program to Create a Directory

A directory is a file system that stores file. Now our task is to create a directory in C#. We can create a directory by using the CreateDirectory() method of the Directory class. This method is used to create directories and subdirectories in a specified path. If the specified directory exists or the given path is invalid then this method will not create a directory. To use CreateDirectory() method we have to import the system.IO namespace in the program.

Syntax:



public static System.IO.DirectoryInfo CreateDirectory (string path);

Parameter: path is the directory path.



Return: This will return the object of the specified created directory.

Exception: It will throw the following exception:

Example:




// C# program to illustrate how
// to create directory
using System;
using System.IO;
  
class GFG{
      
public static void Main()
{
      
    // Create directory named Sravan in C drive
    // Using CreateDirectory() method
    Directory.CreateDirectory("C:\\sravan");
      
    Console.WriteLine("Created");
}
}

Output:

Created

Article Tags :