正确获取磁盘大小
我正在尝试获取已连接 USB 闪存驱动器的物理设备大小。我尝试过使用WMI。
ManagementObjectSearcher mosDisks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE Model = '" + cmbHdd.SelectedItem + "'");
foreach (ManagementObject moDisk in mosDisks.Get())
{
lblCapacity.Text = "Capacity: " + moDisk["Size"];
}
我尝试使用导入来获取几何图形:
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;
它们都返回一个值..但它不正确。
例如,上面的代码返回 250056737280。 当我将整个二进制内容转储到新文件时,FileStream.Length 返回 250059350015
看看最后一个选项更大吗?这也是我的代码按预期工作所需的正确大小。但我无法转储 250GB 数据只是为了获得完整大小。 那么还有其他方法可以获得合适的尺寸吗?
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.
For example, the above code returns 250056737280.
When I dump the entire binary contents to a new file, FileStream.Length returns 250059350015
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以考虑尝试使用 DevideIoControl IOCTL_DISK_GET_LENGTH_INFO。
You might consider trying IOCTL_DISK_GET_LENGTH_INFO with DevideIoControl.
这对你有用吗?
在这里找到上面的示例: http://www.java2s.com/Tutorial/CSharp /0520__Windows/Getfreediskspace.htm
干杯。
贾斯。
Is this any use for you?
Found the above example here: http://www.java2s.com/Tutorial/CSharp/0520__Windows/Getfreediskspace.htm
Cheers.
Jas.