2013-10-04 31 views
4

我有一個程序告訴我所有的硬盤/ USB的,但它只告訴我驅動器號不是名稱。這裏是我有:如何獲得驅動器號和名稱(卷標)

DriveInfo[] drives = DriveInfo.GetDrives(); 
Console.WriteLine("Detected Drives: "); 
for(int i = 0; i < drives.Count(); i++) 
{ 
    Console.WriteLine("Drive " + i + ": " + drives[i].Name); 
} 

return drives; 

這種打印:

Drive 0: C:\ 
Drive 1: E:\ 

,但我想要的名稱,如

Drive 0: C:\ Local disk 
Drive 1: E:\ Kingston USB 

我怎麼弄來的?

+2

@Saravana,鏈接的問題並沒有一個公認的答案也是最高的一票可能不適合OP – Habib

回答

10

您正在尋找VolumeLabel屬性:

http://msdn.microsoft.com/en-us/library/system.io.driveinfo.volumelabel.aspx

例子:

Console.WriteLine(drives[i].VolumeLabel); 
+2

+1。在回答問題時,也很可能不是OP所期待的(期望的輸出似乎在尋找物理驅動器信息,部分被[DriveInfo.DriveType](http://msdn.microsoft.com/ en-us/library/system.io.driveinfo.drivetype.aspx)) –

3

試試這個

try 
     { 
      DriveInfo[] myDrives = DriveInfo.GetDrives(); 

      foreach (DriveInfo drive in myDrives) 
      { 
       Console.WriteLine("Drive:" + drive.Name); 
       Console.WriteLine("Drive Type:" + drive.DriveType); 

       if (drive.IsReady == true) 
       { 
        Console.WriteLine("Vol Label:" + drive.VolumeLabel); 
        Console.WriteLine("File System: " + drive.DriveFormat); 

       } 
      } 
     } 
     catch (Exception) 
     { 

      throw; 
     } 
相關問題