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

การตรวจสอบรูปแบบไดรฟ์ (Drive Format) ใน C#

คุณสมบัติ DriveFormat ของคลาส DriveInfo ใช้สำหรับดูระบบไฟล์ (File System) ของไดรฟ์ที่ระบุ เช่น NTFS, FAT32, exFAT, ext4, APFS ฯลฯ

รูปแบบการใช้งาน

DriveInfo drive = new DriveInfo("C");
string format = drive.DriveFormat; // เช่น "NTFS"

โค้ดตัวอย่างสมบูรณ์

using System;
using System.IO;

public class Demo
{
    public static void Main()
    {
        try
        {
            // ระบุไดรฟ์ที่ต้องการตรวจสอบ (Windows ใช้ตัวอักษรไดรฟ์)
            DriveInfo drive = new DriveInfo("C");
            
            Console.WriteLine($"ไดรฟ์: {drive.Name}");
            Console.WriteLine($"รูปแบบไดรฟ์: {drive.DriveFormat}");
            Console.WriteLine($"ประเภท: {drive.DriveType}");
            Console.WriteLine($"พร้อมใช้งาน: {drive.IsReady}");
            
            if (drive.IsReady)
            {
                Console.WriteLine($"ขนาดทั้งหมด: {drive.TotalSize / (1024.0 * 1024 * 1024):F2} GB");
                Console.WriteLine($"พื้นที่ว่าง: {drive.AvailableFreeSpace / (1024.0 * 1024 * 1024):F2} GB");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"เกิดข้อผิดพลาด: {ex.Message}");
        }
    }
}

ผลลัพธ์ตัวอย่าง

ไดรฟ์: C:\
รูปแบบไดรฟ์: NTFS
ประเภท: Fixed
พร้อมใช้งาน: True
ขนาดทั้งหมด: 476.94 GB
พื้นที่ว่าง: 120.45 GB

รูปแบบไดรฟ์ที่พบบ่อย

ระบบไฟล์ใช้ในคุณสมบัติเด่น
NTFSWindows (ค่าเริ่มต้น)รองรับไฟล์ > 4GB, สิทธิ์การเข้าถึง, การเข้ารหัส, Shadow Copy
FAT32USB Flash Drive, SD Card เก่ารองรับทุก OS, ไฟล์สูงสุด 4GB, โวล्यุมสูงสุด 2TB
exFATUSB Flash Drive ใหม่, SDXCรองรับไฟล์ > 4GB, เบากว่า NTFS, รองรับ Windows/macOS/Linux
ext4Linux (ค่าเริ่มต้น)Journaling, ขนาดไฟล์/โวลยึมใหญ่, Backward compatible
APFSmacOS 10.13+Snapshot, Cloning, Encryption, Space Sharing

การใช้งานข้ามแพลตฟอร์ม

// Linux/macOS ใช้เส้นทาง mount point
DriveInfo drive = new DriveInfo("/"); // root partition
DriveInfo drive = new DriveInfo("/home"); // home partition

ข้อควรระวัง

  • ต้องมีสิทธิ์เข้าถึงไดรฟ์นั้นๆ
  • ไดรฟ์เครือข่าย (Network) อาจไม่รายงาน DriveFormat ได้
  • ควรตรวจสอบ IsReady ก่อนอ่านคุณสมบัติอื่นๆ