ใช้คลาส System.IO.Directory สำหรับสร้าง ย้าย ลบ โฟลเดอร์
ขั้นตอน
- Import namespace:
using System.IO; - ใช้
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) | ย้าย/เปลี่ยนชื่อโฟลเดอร์ |