2008-09-29 40 views

Trả lời

18

Các GetDrives phương pháp() trả về một lớp DriveInfo trong đó có một DriveType tài sản tương ứng với đếm System.IO.DriveType:

public enum DriveType 
{ 
    Unknown,   // The type of drive is unknown. 
    NoRootDirectory, // The drive does not have a root directory. 
    Removable,  // The drive is a removable storage device, 
        // such as a floppy disk drive or a USB flash drive. 
    Fixed,   // The drive is a fixed disk. 
    Network,   // The drive is a network drive. 
    CDRom,   // The drive is an optical disc device, such as a CD 
        // or DVD-ROM. 
    Ram    // The drive is a RAM disk. 
} 

Dưới đây là một ví dụ điều chỉnh nhẹ từ MSDN hiển thị thông tin cho tất cả ổ đĩa:

DriveInfo[] allDrives = DriveInfo.GetDrives(); 
    foreach (DriveInfo d in allDrives) 
    { 
     Console.WriteLine("Drive {0}, Type {1}", d.Name, d.DriveType); 
    } 
+0

FYI 'DriveType' trả về' DriveType.Fixed 'cho ổ cứng USB gắn ngoài. –

4

DriveInfo.DriveType sẽ phù hợp với bạn.

DriveInfo[] allDrives = DriveInfo.GetDrives(); 

foreach (DriveInfo d in allDrives) 
{ 
    Console.WriteLine("Drive {0}", d.Name); 
    Console.WriteLine(" File type: {0}", d.DriveType); 
} 
Các vấn đề liên quan