Firstly, use the DirectoryInfo that operates on Directories. The parameter set in it is the file path −
DirectoryInfo dir = new DirectoryInfo(@"D:\new\");
To get the name of the directory, use the Name property −
dir.Name
The following is the code to display the name of the directory −
Example
using System.IO;
using System;
public class Program {
public static void Main() {
DirectoryInfo dir = new DirectoryInfo(@"D:\new\");
// displaying the name of the directory
Console.WriteLine(dir.Name);
}
}Output
D:\new\