C# - How to Create a Directory?

Learn, how to create a directory or subdirectory using C# program?
Submitted by IncludeHelp, on November 12, 2017 [Last updated : March 26, 2023]

Create a Directory

To create a directory in C#, we use Directory.CreateDirectory() method.

Directory.CreateDirectory()

This is a method of 'Directory' class, which is used to create a directory or sub directory at given path.

Syntax

DirectoryInfo Directory.CreateDirectory(string path);

Parameter(s)

  1. path - It is a location where we need to create directory.

Return Value

This method returns the reference of DirectoryInfo class which is used to perform further operations.

C# program to create a directory

using System;
using System.IO;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      Console.WriteLine("Directory creating:");

      Directory.CreateDirectory("D:/India");
      Console.WriteLine("D:/India");

      Directory.CreateDirectory("D:/India/NCR");
      Console.WriteLine("D:/India/NCR");

      Directory.CreateDirectory("D:/India/NCR/DELHI");
      Console.WriteLine("D:/India/NCR/DELHI");

      Directory.CreateDirectory("D:/India/NCR/NOIDA");
      Console.WriteLine("D:/India/NCR/NOIDA");

      Directory.CreateDirectory("D:/India/NCR/GURUGRAM");
      Console.WriteLine("D:/India/NCR/GURUGRAM");
    }
  }
}

Output

Directory creating:
D:/India
D:/India/NCR
D:/India/NCR/DELHI
D:/India/NCR/NOIDA
D:/India/NCR/GURUGRAM

Explanation

In the above program, we need to remember, when we use "Directory" class, System.IO namespace must be included in the program.

C#.Net Directory Class Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.