Sven Groot said:

You're not supposed to call CloseHandle directly on a SafeHandle. You should call SafeHandle.Close or SafeHandle.Dispose (or just wrap the safe handle in a using block). The way you're doing it, the SafeFileHandle will close the handle again when its finalizer runs, decrementing the handle's reference count once too many, and apparently some FileStream was using that same handle which is now invalidated.

Thanks. I've used your class and modified it so it supports writing and now it works fine. Ta!