public static void FillAllocatedSpaceWithData(DiskImage disk, long startSector, long sectorCount, ref long bytesWritten) { const int DirectTransferSizeLBA = 2048; // 1 MB (assuming 512-byte sectors) byte[] data = new byte[DirectTransferSizeLBA * disk.BytesPerSector]; Random random = new Random(); long sectorIndex = startSector; while (sectorIndex < startSector + sectorCount) { long sectorsToWrite = startSector + sectorCount - sectorIndex; if (sectorsToWrite < DirectTransferSizeLBA) { data = new byte[sectorsToWrite * disk.BytesPerSector]; } random.NextBytes(data); disk.WriteSectors(sectorIndex, data); sectorIndex += DirectTransferSizeLBA; bytesWritten += data.Length; } }