Open In App

C# Program to Demonstrate the Use of CreateSubdirectory Method

Improve
Improve
Like Article
Like
Save
Share
Report

DirectoryInfo class provides different types of methods and properties that are used to perform operations on directories and sub-directories like creating, moving, etc. This class has a CreateSubdirectory() method that is used to create a sub-directory or sub-directories on the given path. Here the given path can be relative to this instance of the DirectoryInfo class.

Syntax:

public System.IO.DirectoryInfo CreateSubdirectory (string spath);

Here, the spath parameter represents the specified path.

Return: It will return the last directory that is specified in the path.

Exceptions:

  • ArgumentException: This exception will occur when the spath does not specify a valid file path or contains invalid DirectoryInfo characters.
  • ArgumentNullException: This exception will occur when the spath is null.
  • DirectoryNotFoundException: This exception will occur when the specified spath is invalid, such as being on an unmapped drive.
  • IOException: This exception will occur when the subdirectory cannot be created.
  • PathTooLongException: This exception will occur when the specified spath, file name, or both exceed the system-defined maximum length.
  • SecurityException: This exception will occur when the caller does not have code access permission to create the directory.
  • NotSupportedException: This exception will occur when the spath contains a colon character (:) which is not the part of the given drive label (e.g. “E:\”).

Example:

C#




// C# program demonstrate the working
// of CreateSubdirectory() method
using System;
using System.IO;
  
class GFG {
  
    static void Main()
    {
  
        // Getting the vignan directory
        DirectoryInfo my_dir = new DirectoryInfo("vignan");
  
        // Creating sub directory named IT
        // in the vignan directory
        // using CreateSubdirectory() method
        my_dir.CreateSubdirectory("IT");
  
        Console.WriteLine("IT  created successfully");
    }
}


Output:

IT  created successfully

Last Updated : 01 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads