C# - How to Create Subdirectory Inside a Directory?

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

Create Subdirectory Inside a Directory

To create subdirectory inside a directory in C#, we use DirectoryInfo.CreateSubdirectory() method.

DirectoryInfo.CreateSubdirectory()

This is a method of "DirectoryInfo" class, it is used to create a sub directory within a directory.

Syntax

DirectoryInfo DirectoryInfo.CreateSubdirectory(string path);

Parameter(s)

  1. path - Sub-Directory name.

Return Value

Returns the object of DirectoryInfo class.

C# program to create subdirectory inside a directory

using System;
using System.IO;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      DirectoryInfo d = new DirectoryInfo("mydir");

      d.CreateSubdirectory("sub-dir");

      Console.WriteLine("Sub-Directory created successfully");

    }
  }
}

Output

Sub-Directory created successfully.

Explanation

In this program we create an object of DirectoryInfo class and initialize with a directory-name, then create sub-directory inside given directory.

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

C#.Net DirectoryInfo Class Programs »

Comments and Discussions!

Load comments ↻





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