获取磁盘大小正确 [英] Getting Disk Size Properly
问题描述
我想获得一个连接的USB闪存驱动器的物理设备大小。我一直在使用WMI尝试。
I am trying to get the physical device size of a connected USB flash drive. I have tried using WMI.
ManagementObjectSearcher mosDisks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE Model = '" + cmbHdd.SelectedItem + "'");
foreach (ManagementObject moDisk in mosDisks.Get())
{
lblCapacity.Text = "Capacity: " + moDisk["Size"];
}
我一直在使用进口来获取几何尝试:
I have tried using imports to get the geometry:
var geo = new DiskGeometry();
uint returnedBytes;
DeviceIoControl(Handle, 0x70000, IntPtr.Zero, 0, ref geo, (uint)Marshal.SizeOf(typeof(DiskGeometry)), out returnedBytes, IntPtr.Zero);
return geo.DiskSize;
他们都做返回一个值..但它是不正确的。
They all do return a value.. but it is not correct.
例如,上面的代码返回250056737280.
当我转储整个二进制内容到一个新的文件,FileStream.Length返回250059350015
For example, the above code returns 250056737280. When I dump the entire binary contents to a new file, FileStream.Length returns 250059350015
见的最后一个选项是如何做大?这也是corrrect大小,我需要获得预期我的代码工作。但我不能转储数据的250GB只是为了获得全尺寸。 ?
那么,有没有其他方法来获得合适的尺寸
See how the last option is bigger? That is also the corrrect size I need to get for my code to work as expected. But I cannot dump 250gb of data just to get the full size. So is there another method to get proper size?
推荐答案
您可能会考虑尝试的 IOCTL_DISK_GET_LENGTH_INFO ,提供DevideIoControl。
You might consider trying IOCTL_DISK_GET_LENGTH_INFO with DevideIoControl.
这篇关于获取磁盘大小正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!