如何獲取系統上的邏輯驅動器(C#)列表以及其容量和可用空間?獲取邏輯驅動器列表
32
A
回答
50
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
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.
}
相關問題
- 1. 獲取邏輯驅動器
- 2. 邏輯驅動器信息
- 3. C# - 獲取物理驅動器(不是邏輯驅動器)的列表或數量
- 4. 如何在Windows 10中獲取邏輯驅動器名稱?
- 5. 獲取包括共享RDP驅動器的驅動器列表
- 6. 邏輯驅動器作爲DVD驅動器
- 7. 獲取邏輯列字符
- 8. 邏輯驅動器字母不顯示
- 9. 如何列出沒有映射驅動器的邏輯驅動器
- 10. 動態列表的邏輯
- 11. 有效的方法來獲取硬盤驅動器的所有邏輯驅動器號並收集根目錄
- 12. 列表列邏輯
- 13. 獲取帶有JavaScript的可移動驅動器列表?
- 14. 獲取共有序列的邏輯
- 15. LINQ列表邏輯
- 16. PowerShell的獲得驅動器列表
- 17. 域驅動設計 - 邏輯刪除
- 18. 從父子表獲取邏輯數據?
- 19. 雙鏈表列表邏輯
- 20. 獲取頁數的邏輯
- 21. 谷歌驅動器api獲取文件更新列表
- 22. 如何獲取驅動器號在Powershell 2.0中的列表
- 23. 如何從谷歌驅動器api v3獲取文件列表?
- 24. 聯網驅動器列表
- 25. 系統驅動器列表
- 26. 如何獲取物理和邏輯磁盤的列表?
- 27. MySQL查詢使用邏輯操作獲取數據列表
- 28. 如何獲取驅動器標籤和驅動器序列號QB64
- 29. 枚舉perticule物理設備名稱的邏輯驅動器
- 30. 德爾福得到的邏輯驅動器
這是在.NET的最新版本中添加的新東西。我寫了一個小應用程序來展示這些年前的事情,但當時必須走WMI路線。非常方便知道反正...乾杯 – 2009-04-23 14:18:10
完美...謝謝 – PaulB 2009-04-23 14:20:53
快速查看MSDN:被添加到.NET 2.0中。 – Richard 2009-04-23 14:22:19