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

วิธีสร้าง Directory (โฟลเดอร์) ด้วย C#

ใช้คลาส System.IO.Directory สำหรับสร้าง ย้าย ลบ โฟลเดอร์

ขั้นตอน

  1. Import namespace: using System.IO;
  2. ใช้ Directory.CreateDirectory()

ตัวอย่าง: สร้างโฟลเดอร์

string myDir = @"D:\NEW";
if (!Directory.Exists(myDir))
{
    Directory.CreateDirectory(myDir);
}

ตัวอย่าง: สร้าง Sub-directory

string mySubDir = @"C:\NEW\my\sub";
Directory.CreateDirectory(mySubDir); // สร้างพร้อม Parent directories

เมธอดสำคัญของ Directory Class

เมธอดคำอธิบาย
CreateDirectory(path)สร้างโฟลเดอร์ (รวม Parent)
Delete(path)ลบโฟลเดอร์ว่าง
Delete(path, true)ลบโฟลเดอร์พร้อมไฟล์/ซับโฟลเดอร์
Exists(path)ตรวจสอบว่ามีอยู่หรือไม่
GetDirectories(path)ดึงรายการซับโฟลเดอร์
GetFiles(path)ดึงรายการไฟล์
Move(source, dest)ย้าย/เปลี่ยนชื่อโฟลเดอร์