public DriveAccess(string Path) { if (Path == null || Path.Length == 0) throw new ArgumentNullException("Path"); driveHandle = CreateFile(Path, 0xC0000000, 0x03, IntPtr.Zero, 0x03, 0x80, IntPtr.Zero); if (driveHandle.IsInvalid) { driveHandle.Close(); driveHandle.Dispose(); driveHandle = null; throw new Exception("Win32 Exception : 0x" + Convert.ToString(Marshal.GetHRForLastWin32Error(), 16).PadLeft(8, '0')); } driveStream = new FileStream(driveHandle, FileAccess.ReadWrite); IntPtr p = Marshal.AllocHGlobal(24); uint returned; if (DeviceIoControl(driveHandle.DangerousGetHandle(), 0x00070000, IntPtr.Zero, 0, p, 40, out returned, IntPtr.Zero)) unsafe { driveGeometry = new DriveGeometry((byte*)p.ToPointer()); } else { Marshal.FreeHGlobal(p); throw new Exception("Could not get the drive geometry information!"); } Marshal.FreeHGlobal(p); }
public DriveAccess(string Path) { if (Path == null || Path.Length == 0) { throw new ArgumentNullException("Path"); } driveHandle = CreateFile(Path, 0xC0000000, 0x03, IntPtr.Zero, 0x03, 0x80, IntPtr.Zero); if (driveHandle.IsInvalid) { driveHandle.Close(); driveHandle.Dispose(); driveHandle = null; throw new Exception("Win32 Exception : 0x" + Convert.ToString(Marshal.GetHRForLastWin32Error(), 16).PadLeft(8, '0')); } driveStream = new FileStream(driveHandle, FileAccess.ReadWrite); IntPtr p = Marshal.AllocHGlobal(24); uint returned; if (DeviceIoControl(driveHandle.DangerousGetHandle(), 0x00070000, IntPtr.Zero, 0, p, 40, out returned, IntPtr.Zero)) { unsafe { driveGeometry = new DriveGeometry((byte *)p.ToPointer()); } } else { Marshal.FreeHGlobal(p); throw new Exception("Could not get the drive geometry information!"); } Marshal.FreeHGlobal(p); }