C# 获取逻辑驱动器列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/781905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting a list of logical drives
提问by PaulB
How can I get the list of logial drives (C#) on a system as well as their capacity and free space?
如何获取系统上的逻辑驱动器 (C#) 列表及其容量和可用空间?
回答by Eoin Campbell
You can retrieve this information with Windows Management Instrumentation (WMI)
您可以使用 Windows Management Instrumentation (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());
}
Theres more info here about the attribute you can poll
这里有关于您可以轮询的属性的更多信息
http://www.geekpedia.com/tutorial233_Getting-Disk-Drive-Information-using-WMI-and-Csharp.html
http://www.geekpedia.com/tutorial233_Getting-Disk-Drive-Information-using-WMI-and-Csharp.html
回答by Tom Ritter
Their example has more robust, but here's the crux of it
他们的例子更强大,但这里是它的关键
string[] drives = System.IO.Directory.GetLogicalDrives();
foreach (string str in drives)
{
System.Console.WriteLine(str);
}
You could also P/Invokeand call the win32 function (or use it if you're in unmanaged code).
您还可以P/Invoke并调用 win32 函数(或者如果您在非托管代码中使用它)。
That only gets a list of the drives however, for information about each one, you would want to use GetDrivesas Chris Ballance demonstrates.
但是,这只会获取驱动器的列表,有关每个驱动器的信息,您可能希望像 Chris Ballance 演示的那样使用GetDrives。
回答by Chris Ballance
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);
}
回答by mehrdad
maybe this is what you want:
也许这就是你想要的:
listBox1.Items.Clear();
foreach (DriveInfo f in DriveInfo.GetDrives())
listBox1.Items.Add(f);
回答by SpoiledTechie.com
This is a wonderful piece of code.
这是一段美妙的代码。
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.
}