Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

โปรแกรม C# แสดงชื่อไดเรกทอรี

ใช้ DirectoryInfo.Name Property ดึงชื่อโฟลเดอร์ (ไม่รวมพาธเต็ม)

ตัวอย่างโค้ด

using System;
using System.IO;

public class Program
{
    public static void Main()
    {
        DirectoryInfo dir = new DirectoryInfo(@"D:\new\");
        Console.WriteLine(dir.Name); // "new"
    }
}

ผลลัพธ์

new

คุณสมบัติอื่น: FullName (พาธเต็ม), Parent (โฟลเดอร์แม่), Root (รูท), Exists (มีอยู่จริงไหม)