public static extern bool DeviceIoControl( Microsoft.Win32.SafeHandles.SafeFileHandle hDevice, uint IoControlCode, IntPtr InBuffer, uint nInBufferSize, ref DiskGeometry OutBuffer, int nOutBufferSize, out uint pBytesReturned, IntPtr Overlapped );
public static bool GetDriveGeometry(SafeFileHandle hDevice, ref DiskGeometry pdg) { uint junk; // discard results return Native.DeviceIoControl( hDevice, // device to be queried Native.IOCTL_DISK_GET_DRIVE_GEOMETRY, // operation to perform IntPtr.Zero, 0, // no input buffer ref pdg, Marshal.SizeOf(pdg), // output buffer out junk, // # bytes returned IntPtr.Zero); // synchronous I/O }
public static long GetDriveLength(SafeFileHandle hDevice) { DiskGeometry pdg = new DiskGeometry(); if (GetDriveGeometry(hDevice, ref pdg)) { Console.WriteLine(pdg.Cylinders + " " + pdg.TracksPerCylinder + " " + pdg.SectorsPerTrack + " " + pdg.BytesPerSector); return (long)pdg.Cylinders * (long)pdg.TracksPerCylinder * (long)pdg.SectorsPerTrack * (long)pdg.BytesPerSector; } else throw new Win32Exception(); }