internal static void WriteSectorDataToHandle(IntPtr handle, byte[] data, ulong startSector, ulong numSectors, ulong sectorSize, int length) { uint bytesWritten; bool result; LARGE_INTEGER li; li.LowPart = 0; li.QuadPart = (long)(startSector * sectorSize); NativeDisk.SetFilePointer(handle, li.LowPart, out li.HighPart, EMoveMethod.Begin); result = NativeDisk.WriteFile(handle, data, (uint)(length), out bytesWritten, IntPtr.Zero); if (!result) { var exception = new Win32Exception(Marshal.GetLastWin32Error()); throw new Exception(string.Format("Error occured when trying to write sector data to handle.\nError code: {0}\nMessage: {1}", exception.NativeErrorCode, exception.Message)); } }