public override void Flush() { if (!NativeFile.FlushFileBuffers(this.handle)) { throw new IOException("Unable to flush stream", NativeFileStream.MarshalGetLastWin32Error()); } }
public override long Seek(long offset, SeekOrigin origin) { long distanceToMoveHigh; if (!NativeFile.SetFilePointerEx(this.handle, offset, out distanceToMoveHigh, origin)) { throw new IOException("Unable to seek to this position", NativeFileStream.MarshalGetLastWin32Error()); } this.position = distanceToMoveHigh; return(this.position); }
public unsafe int Read(IntPtr buffer, int offset, int count) { if (buffer == IntPtr.Zero) { throw new ArgumentNullException("buffer"); } int numberOfBytesRead; if (!NativeFile.ReadFile(this.handle, (IntPtr)((void *)((IntPtr)(void *)buffer + offset)), count, out numberOfBytesRead, IntPtr.Zero)) { throw new IOException("Unable to read from file", NativeFileStream.MarshalGetLastWin32Error()); } this.position += (long)numberOfBytesRead; return(numberOfBytesRead); }
public override void SetLength(long value) { long distanceToMoveHigh; if (!NativeFile.SetFilePointerEx(this.handle, value, out distanceToMoveHigh, SeekOrigin.Begin)) { throw new IOException("Unable to seek to this position", NativeFileStream.MarshalGetLastWin32Error()); } if (!NativeFile.SetEndOfFile(this.handle)) { throw new IOException("Unable to set the new length", NativeFileStream.MarshalGetLastWin32Error()); } if (this.position < value) { this.Seek(this.position, SeekOrigin.Begin); } else { this.Seek(0L, SeekOrigin.End); } }
public NativeFileStream(string fileName, NativeFileMode fileMode, NativeFileAccess access, NativeFileShare share = NativeFileShare.Read) { this.handle = NativeFile.Create(fileName, access, share, IntPtr.Zero, fileMode, NativeFileOptions.None, IntPtr.Zero); if (this.handle == new IntPtr(-1)) { int lastWin32Error = NativeFileStream.MarshalGetLastWin32Error(); if (lastWin32Error == 2) { throw new FileNotFoundException("Unable to find file", fileName); } Result resultFromWin32Error = Result.GetResultFromWin32Error(lastWin32Error); throw new IOException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Unable to open file {0}", new object[1] { (object)fileName }), resultFromWin32Error.Code); } else { this.canRead = (NativeFileAccess)0 != (access & NativeFileAccess.Read); this.canWrite = (NativeFileAccess)0 != (access & NativeFileAccess.Write); this.canSeek = true; } }