Just use the WriteFile P/Invoke function:
[DllImport("kernel32.dll", SetLastError=true)]
static extern bool WriteFile(
IntPtr hFile,
byte[] lpBuffer,
uint nNumberOfBytesToWrite,
out uint lpNumberOfBytesWritten,
IntPtr lpOverlapped);
Replace the ReadFile calls by WriteFile, that's pretty easy. Just keep in mind:
- the handle you got using
CreateFile has a write access
- you have to write bytes per chunk; chunk size must be a multiple of the sector size. I.e. if you want to write only 1 byte, you'll have to read a whole sector, modify the byte in memory, and write back the whole sector