title: Getting Disk Size Properly
body: <p>I am trying to get the physical device size of a connected USB flash drive. I have tried using WMI.</p>
  
  <pre><code>        ManagementObjectSearcher mosDisks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE Model = '" + cmbHdd.SelectedItem + "'");
          foreach (ManagementObject moDisk in mosDisks.Get())
          {
              lblCapacity.Text = "Capacity: " + moDisk["Size"];
          }
  </code></pre>
  
  <p>I have tried using imports to get the geometry:</p>
  
  <pre><code>        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;
  </code></pre>
  
  <p>They all do return a value.. but it is not correct.</p>
  
  <p>For example, the above code returns 250056737280.
  When I dump the entire binary contents to a new file, FileStream.Length returns 250059350015</p>
  
  <p>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?</p>