2009-04-23 60 views

回答

50
+0

這是在.NET的最新版本中添加的新東西。我寫了一個小應用程序來展示這些年前的事情,但當時必須走WMI路線。非常方便知道反正...乾杯 – 2009-04-23 14:18:10

+0

完美...謝謝 – PaulB 2009-04-23 14:20:53

+0

快速查看MSDN:被添加到.NET 2.0中。 – Richard 2009-04-23 14:22:19

0

可以檢索與Windows管理規範(WMI)

using System.Management; 

    ManagementObjectSearcher mosDisks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive"); 
    // Loop through each object (disk) retrieved by WMI 
    foreach (ManagementObject moDisk in mosDisks.Get()) 
    { 
     // Add the HDD to the list (use the Model field as the item's caption) 
     Console.WriteLine(moDisk["Model"].ToString()); 
    } 

即使世界更多的信息在這裏有關屬性此信息,您可查詢

http://www.geekpedia.com/tutorial233_Getting-Disk-Drive-Information-using-WMI-and-Csharp.html

18

Directory.GetLogicalDrives

他們的榜樣擁有更強大的,但這裏是它

  string[] drives = System.IO.Directory.GetLogicalDrives(); 

      foreach (string str in drives) 
      { 
       System.Console.WriteLine(str); 
      } 

P/Invoke,並可以調用Win32函數(或者使用它,如果你在非託管代碼是)的關鍵所在。

這隻能得到一個驅動器列表,然而,關於每一個的信息,你會想要使用GetDrives克里斯巴蘭斯演示。

24
foreach (var drive in DriveInfo.GetDrives()) 
{ 
    double freeSpace = drive.TotalFreeSpace; 
    double totalSpace = drive.TotalSize; 
    double percentFree = (freeSpace/totalSpace) * 100; 
    float num = (float)percentFree; 

    Console.WriteLine("Drive:{0} With {1} % free", drive.Name, num); 
    Console.WriteLine("Space Remaining:{0}", drive.AvailableFreeSpace); 
    Console.WriteLine("Percent Free Space:{0}", percentFree); 
    Console.WriteLine("Space used:{0}", drive.TotalSize); 
    Console.WriteLine("Type: {0}", drive.DriveType); 
} 
3

也許這就是你想要什麼:

listBox1.Items.Clear(); 

foreach (DriveInfo f in DriveInfo.GetDrives())  
    listBox1.Items.Add(f); 
0

這是一個美妙的一段代碼。

ObjectQuery query = 
    new ObjectQuery("SELECT * FROM Win32_LogicalDisk WHERE DriveType=3"); // Create query to select all the hdd's 

ManagementObjectSearcher searcher = 
    new ManagementObjectSearcher(scope, query); // run the query 

ManagementObjectCollection queryCollection = searcher.Get(); // get the results 
string sVolumeLabel = ""; 
string[,] saReturn = new string[queryCollection.Count, 7]; 
int i = 0; // counter for foreach 

foreach (ManagementObject m in queryCollection) 
{ 
    if (string.IsNullOrEmpty(Convert.ToString(m["VolumeName"]))) { sVolumeLabel = "Local Disk"; } else { sVolumeLabel = Convert.ToString(m["VolumeName"]); } // Disk Label 
    string sSystemName = Convert.ToString(m["SystemName"]); // Name of computer 
    string sDriveLetter = Convert.ToString(m["Name"]); // Drive Letter 

    decimal dSize = Math.Round((Convert.ToDecimal(m["Size"])/1073741824), 2); //HDD Size in Gb 
    decimal dFree = Math.Round((Convert.ToDecimal(m["FreeSpace"])/1073741824), 2); // Free Space in Gb 
    decimal dUsed = dSize - dFree; // Used HDD Space in Gb 

    int iPercent = Convert.ToInt32((dFree/dSize) * 100); // Percentage of free space 

    saReturn[i,0] = sSystemName; 
    saReturn[i,1] = sDriveLetter; 
    saReturn[i,2] = sVolumeLabel; 
    saReturn[i,3] = Convert.ToString(dSize); 
    saReturn[i,4] = Convert.ToString(dUsed); 
    saReturn[i,5] = Convert.ToString(dFree); 
    saReturn[i,6] = Convert.ToString(iPercent); 

    i++; // increase counter. This will add the above details for the next drive. 
}