/// <summary> /// Opens this alternate data stream. /// </summary> /// <param name="mode"> /// A <see cref="FileMode"/> value that specifies whether a stream is created if one does not exist, /// and determines whether the contents of existing streams are retained or overwritten. /// </param> /// <param name="access"> /// A <see cref="FileAccess"/> value that specifies the operations that can be performed on the stream. /// </param> /// <param name="share"> /// A <see cref="FileShare"/> value specifying the type of access other threads have to the file. /// </param> /// <param name="bufferSize"> /// The size of the buffer to use. /// </param> /// <param name="useAsync"> /// <see langword="true"/> to enable async-IO; /// otherwise, <see langword="false"/>. /// </param> /// <returns> /// A <see cref="FileStream"/> for this alternate data stream. /// </returns> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="bufferSize"/> is less than or equal to zero. /// </exception> /// <exception cref="SecurityException"> /// The caller does not have the required permission. /// </exception> /// <exception cref="UnauthorizedAccessException"> /// The caller does not have the required permission, or the file is read-only. /// </exception> /// <exception cref="IOException"> /// The specified file is in use. /// </exception> /// <exception cref="ArgumentException"> /// The path of the stream is invalid. /// </exception> /// <exception cref="Win32Exception"> /// There was an error opening the stream. /// </exception> public FileStream Open(FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync) { if (0 >= bufferSize) { throw new ArgumentOutOfRangeException(/*nameof*/ (bufferSize.ToString()), bufferSize, null); } #if NET35 FileIOPermissionAccess permAccess = CalculateAccess(mode, access); new FileIOPermission(permAccess, FilePath).Demand(); #endif SafeNativeMethods.NativeFileFlags flags = useAsync ? SafeNativeMethods.NativeFileFlags.Overlapped : 0; var handle = SafeNativeMethods.SafeCreateFile(FullPath, access.ToNative(), share, IntPtr.Zero, mode, flags, IntPtr.Zero); if (handle.IsInvalid) { SafeNativeMethods.ThrowLastIOError(FullPath); } return(new FileStream(handle, access, bufferSize, useAsync)); }
/// <summary> /// Opens this alternate data stream. /// </summary> /// <param name="mode"> /// A <see cref="FileMode"/> value that specifies whether a stream is created if one does not exist, /// and determines whether the contents of existing streams are retained or overwritten. /// </param> /// <param name="access"> /// A <see cref="FileAccess"/> value that specifies the operations that can be performed on the stream. /// </param> /// <param name="share"> /// A <see cref="FileShare"/> value specifying the type of access other threads have to the file. /// </param> /// <param name="bufferSize"> /// The size of the buffer to use. /// </param> /// <param name="useAsync"> /// <see langword="true"/> to enable async-IO; /// otherwise, <see langword="false"/>. /// </param> /// <returns> /// A <see cref="FileStream"/> for this alternate data stream. /// </returns> /// <exception cref="ArgumentOutOfRangeException"> /// <paramref name="bufferSize"/> is less than or equal to zero. /// </exception> /// <exception cref="SecurityException"> /// The caller does not have the required permission. /// </exception> /// <exception cref="UnauthorizedAccessException"> /// The caller does not have the required permission, or the file is read-only. /// </exception> /// <exception cref="IOException"> /// The specified file is in use. /// </exception> /// <exception cref="ArgumentException"> /// The path of the stream is invalid. /// </exception> /// <exception cref="Win32Exception"> /// There was an error opening the stream. /// </exception> public FileStream Open(FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync) { if (0 >= bufferSize) { throw new ArgumentOutOfRangeException("bufferSize", bufferSize, null); } FileIOPermissionAccess permAccess = CalculateAccess(mode, access); new FileIOPermission(permAccess, _filePath).Demand(); SafeNativeMethods.NativeFileFlags flags = useAsync ? SafeNativeMethods.NativeFileFlags.Overlapped : 0; var handle = SafeNativeMethods.SafeCreateFile(this.FullPath, SafeNativeMethods.ToNative(access), share, IntPtr.Zero, mode, flags, IntPtr.Zero); if (handle.IsInvalid) { SafeNativeMethods.ThrowLastWin32Error(); } return(new FileStream(handle, access, bufferSize, useAsync)); }