2009-01-05 21 views

回答

67

對於大多數信息,您可以使用DriveInfo類。

using System; 
using System.IO; 

class Info { 
    public static void Main() { 
     DriveInfo[] drives = DriveInfo.GetDrives(); 
     foreach (DriveInfo drive in drives) { 
      //There are more attributes you can use. 
      //Check the MSDN link for a complete example. 
      Console.WriteLine(drive.Name); 
      if (drive.IsReady) Console.WriteLine(drive.TotalSize); 
     } 
    } 
} 
+1

不錯,謝謝! – leo 2009-01-05 09:37:14

+1

除了本地計算機以外的其他計算機上的驅動器信息如何? – flipdoubt 2009-01-05 12:13:40

2

檢查DriveInfo類,看它是否包含您需要的所有信息。

5

怎麼樣安裝卷,那就是你有沒有盤符?

foreach(ManagementObject volume in 
      new ManagementObjectSearcher("Select * from Win32_Volume").Get()) 
{ 
    if(volume["FreeSpace"] != null) 
    { 
    Console.WriteLine("{0} = {1} out of {2}", 
        volume["Name"], 
        ulong.Parse(volume["FreeSpace"].ToString()).ToString("#,##0"), 
        ulong.Parse(volume["Capacity"].ToString()).ToString("#,##0")); 
    } 
} 
3

如果你想在本地計算機,以獲取信息爲單/特定的驅動器。你可以使用DriveInfo類別:

//C Drive Path, this is useful when you are about to find a Drive root from a Location Path. 
string path = "C:\\Windows"; 

//Find its root directory i.e "C:\\" 
string rootDir = Directory.GetDirectoryRoot(path); 

//Get all information of Drive i.e C 
DriveInfo driveInfo = new DriveInfo(rootDir); //you can pass Drive path here e.g DriveInfo("C:\\") 

long availableFreeSpace = driveInfo.AvailableFreeSpace; 
string driveFormat = driveInfo.DriveFormat; 
string name = driveInfo.Name; 
long totalSize = driveInfo.TotalSize;