C# Program To Demonstrate The Use Of The Createsubdirectory Method

In this article, we will discuss a C# program to illustrate the use of the CreateSubdirectory method in C#. But before going to its implementation, we must know about the CreateSubdirectory method.

What is the CreateSubdirectory method?

In the .NET Framework, the C# Directory class offers static methods for creating subdirectories and other directories, as well as copying, moving, and deleting them. Before using the Directory class, we must import the System.IO namespace. The DirectoryInfo class is part of the System.IO namespace that is used to create, delete, and move directories. Methods are available to execute operations on directories and subdirectories.

The DirectoryInfo class contains different methods to perform different operations. As shown below, this class contains the CreateSubdirectory method for creating a subdirectory. Here, the method shown is applies to this example of the DirectoryInfo class.

Syntax:

It has the following syntax:

Example

public System.IO.DirectoryInfo CreateSubDirectory(String source_path);

Here, source_path refers to the specified path.

Return value: It will return the last directory of the path.

Exception:

ArgumentException: It raises this exception if the source_path does not have a valid file path or it contains the invalid DirectoryInfo .

ArgumentNullException: If the given source path is null, it raises this exception.

DirectoryNotFoundException: If the path mentioned is invalid, it arises.

IOException: The IO exception is raised if the subdirectory is not created.

PathTooLongException: This exception is raised if the specified path, file name, or both exceed a system-defined maximum length.

SecurityException: If the caller function doesn't have access to the code.

NotSupportedException: If the path has a semicolon (J, it is not part of the specified drive label (for example, "E:\").

Example:

Let us take a program to implement the CreateSubdirectory method in C#.

Example

using System; 
using System.IO; 

class CreateSubDirectory{ 

	static void Main() 
	{ 

		// getting the specified directory
		DirectoryInfo main_dir = new DirectoryInfo("Programs"); 

		// The subdirectory named "C#_Programs is 
		//created under the Programs directory"
		main_dir.CreateSubdirectory("C#_Programs"); 

		Console.WriteLine("C#_Programs subdirectory is created successfully."); 
	} 
}

Output:

Output

C#_Programs subdirectory is created successfully.

Input Required

This code uses input(). Please provide values below: