VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/c-sharp-program-to-create-a-directory/

⇱ C# Program to Create a Directory - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C# Program to Create a Directory

Last Updated : 30 Nov, 2021

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:

  • IOException: This exception occurs when the directory specified by path is a file.
  • UnauthorizedAccessException: This exception occurs when the caller does not have the required permission.
  • ArgumentException: This exception occurs when the path is prefixed with, or contains, only a colon character (:).
  • ArgumentNullException: This exception occurs when the path is null.
  • PathTooLongException: This exception occurs when the specified path, file name, or both exceed the system-defined maximum length.
  • DirectoryNotFoundException: This exception occurs when the specified path is invalid 
  • NotSupportedException: This exception occurs when the path contains a colon character(:) that is not part of a drive label ("D:\").

Example:

Output:

Created
👁 Image
Comment
Article Tags:

Explore