2011-03-04 39 views

回答

13

要獲得驅動器的列表,你可以使用System.IO.DriveInfo類:

foreach(var drive in DriveInfo.GetDrives()) 
{ 
    Console.WriteLine("Drive Type: {0}", drive.DriveType); 
    Console.WriteLine("Drive Size: {0}", drive.TotalSize); 
    Console.WriteLine("Drive Free Space: {0}", drive.TotalFreeSpace); 
} 

不幸的是,這並沒有提供一種方式來傾聽USB鑰匙的插入。還有另外一個問題處理,你可以看看:

.NET - Detecting USB Drive Insertion and Removal...

0

這裏是回答你的第一個問題。要獲得所有的邏輯驅動器,你可以試試這個:

string[] drives = Environment.GetLogicalDrives(); 
0

System.IO.DriveInfo類是開始的地方。 DriveType屬性可以告訴你你在找什麼。

2

你可以在驅動器和信息很容易

DriveInfo[] drives = DriveInfo.GetDrives(); 

foreach (DriveInfo drive in drives) 
{ 
    Console.WriteLine(drive.Name); 
    Console.WriteLine(drive.TotalSize); 
} 

有在檢測添加/刪除驅動器on CodeProject

+0

好像我太慢了...... – 2011-03-04 15:32:20

+0

@Phil不在它多! – 2011-03-04 15:48:47

3
 public string getalldrivestotalnfreespace() 
    { 
     string s = " Drive   Free Space TotalSpace  FileSystem %Free Space  DriveType\n\r========================================================================================\n\r"; 
     foreach (DriveInfo drive in DriveInfo.GetDrives()) 
     { 
      double ts = 0; 
      double fs = 0; 
      double frprcntg = 0; 
      long divts = 1024 * 1024 * 1024; 
      long divfs = 1024 * 1024 * 1024; 
      string tsunit = "GB"; 
      string fsunit = "GB"; 
      if (drive.IsReady) 
      { 
       fs = drive.TotalFreeSpace; 
       ts = drive.TotalSize; 
       frprcntg = (fs/ts) * 100; 
       if (drive.TotalSize < 1024) 
       { 
        divts =1; tsunit = "Byte(s)"; 
       } 
       else if (drive.TotalSize < (1024*1024)) 
       { 
        divts = 1024; tsunit = "KB"; 
       } 
       else if (drive.TotalSize < (1024 * 1024*1024)) 
       { 
        divts = 1024*1024; tsunit = "MB"; 
       } 
       //---------------------- 
       if (drive.TotalFreeSpace < 1024) 
       { 
        divfs = 1; fsunit = "Byte(s)"; 
       } 
       else if (drive.TotalFreeSpace < (1024 * 1024)) 
       { 
        divfs = 1024; fsunit = "KB"; 
       } 
       else if (drive.TotalFreeSpace < (1024 * 1024 * 1024)) 
       { 
        divfs = 1024 * 1024; fsunit = "MB"; 
       } 
       s = s + 
       " " + drive.VolumeLabel.ToString() + 
       "[" + drive.Name.Substring(0, 2) + 
       "]\t" + String.Format("{0,10:0.0}", ((fs/divfs)).ToString("N2")) + fsunit + 
       String.Format("{0,10:0.0}", (ts/divts).ToString("N2")) + tsunit + 
       "\t" + drive.DriveFormat.ToString() + "\t\t" + frprcntg.ToString("N2") + "%"+ 
       "\t\t" + drive.DriveType.ToString(); 

       s = s + "\n\r"; 
      } 
     } 
     return s; 
    } 

的輸出中應該是這樣的: - enter image description here

+1

只需提及:只需上傳圖片即可在圖片中添加圖片(點擊「插入圖片」圖標,這樣它就可以以內聯方式顯示,而不需要關注鏈接。 – Mic 2013-03-08 21:46:28

相關問題