ประการแรก ใช้ DirectoryInfo ที่ทำงานบนไดเร็กทอรี พารามิเตอร์ที่ตั้งไว้คือเส้นทางของไฟล์ -
DirectoryInfo dir = new DirectoryInfo(@"D:\new\");
ในการรับชื่อของไดเร็กทอรี ใช้คุณสมบัติ Name -
dir.Name
ต่อไปนี้เป็นรหัสสำหรับแสดงชื่อไดเร็กทอรี -
ตัวอย่าง
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);
}
} ผลลัพธ์
D:\new\