正确获取磁盘大小

发布于 2024-09-28 19:04:16 字数 849 浏览 0 评论 0原文

我正在尝试获取已连接 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

平定天下 2024-10-05 19:04:16

您可以考虑尝试使用 DevideIoControl IOCTL_DISK_GET_LENGTH_INFO

You might consider trying IOCTL_DISK_GET_LENGTH_INFO with DevideIoControl.

若能看破又如何 2024-10-05 19:04:16

这对你有用吗?

using System;
using System.Runtime.InteropServices;

public class MainClass
{
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
       out ulong lpFreeBytesAvailable,
       out ulong lpTotalNumberOfBytes,
       out ulong lpTotalNumberOfFreeBytes);
    public static void Main()
    {
        ulong freeBytesAvail;
        ulong totalNumOfBytes;
        ulong totalNumOfFreeBytes;

        if (!GetDiskFreeSpaceEx("C:\\", out freeBytesAvail, out totalNumOfBytes, out totalNumOfFreeBytes))
        {
            Console.Error.WriteLine("Error occurred: {0}",
                Marshal.GetExceptionForHR(Marshal.GetLastWin32Error()).Message);
        }
        else
        {
            Console.WriteLine("Free disk space:");
            Console.WriteLine("    Available bytes : {0}", freeBytesAvail);
            Console.WriteLine("    Total # of bytes: {0}", totalNumOfBytes);
            Console.WriteLine("    Total free bytes: {0}", totalNumOfFreeBytes);
        }
    }
}

在这里找到上面的示例: http://www.java2s.com/Tutorial/CSharp /0520__Windows/Getfreediskspace.htm

干杯。
贾斯。

Is this any use for you?

using System;
using System.Runtime.InteropServices;

public class MainClass
{
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
       out ulong lpFreeBytesAvailable,
       out ulong lpTotalNumberOfBytes,
       out ulong lpTotalNumberOfFreeBytes);
    public static void Main()
    {
        ulong freeBytesAvail;
        ulong totalNumOfBytes;
        ulong totalNumOfFreeBytes;

        if (!GetDiskFreeSpaceEx("C:\\", out freeBytesAvail, out totalNumOfBytes, out totalNumOfFreeBytes))
        {
            Console.Error.WriteLine("Error occurred: {0}",
                Marshal.GetExceptionForHR(Marshal.GetLastWin32Error()).Message);
        }
        else
        {
            Console.WriteLine("Free disk space:");
            Console.WriteLine("    Available bytes : {0}", freeBytesAvail);
            Console.WriteLine("    Total # of bytes: {0}", totalNumOfBytes);
            Console.WriteLine("    Total free bytes: {0}", totalNumOfFreeBytes);
        }
    }
}

Found the above example here: http://www.java2s.com/Tutorial/CSharp/0520__Windows/Getfreediskspace.htm

Cheers.
Jas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文