public FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options)
{
if (path == null)
throw new ArgumentNullException(nameof(path), SR.ArgumentNull_Path);
if (path.Length == 0)
throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
// don't include inheritable in our bounds check for share
FileShare tempshare = share & ~FileShare.Inheritable;
string badArg = null;
if (mode < FileMode.CreateNew || mode > FileMode.Append)
badArg = nameof(mode);
else if (access < FileAccess.Read || access > FileAccess.ReadWrite)
badArg = nameof(access);
else if (tempshare < FileShare.None || tempshare > (FileShare.ReadWrite | FileShare.Delete))
badArg = nameof(share);
if (badArg != null)
throw new ArgumentOutOfRangeException(badArg, SR.ArgumentOutOfRange_Enum);
// NOTE: any change to FileOptions enum needs to be matched here in the error validation
if (options != FileOptions.None && (options & ~(FileOptions.WriteThrough | FileOptions.Asynchronous | FileOptions.RandomAccess | FileOptions.DeleteOnClose | FileOptions.SequentialScan | FileOptions.Encrypted | (FileOptions)0x20000000 /* NoBuffering */)) != 0)
throw new ArgumentOutOfRangeException(nameof(options), SR.ArgumentOutOfRange_Enum);
if (bufferSize <= 0)
throw new ArgumentOutOfRangeException(nameof(bufferSize), SR.ArgumentOutOfRange_NeedPosNum);
// Write access validation
if ((access & FileAccess.Write) == 0)
{
if (mode == FileMode.Truncate || mode == FileMode.CreateNew || mode == FileMode.Create || mode == FileMode.Append)
{
// No write access, mode and access disagree but flag access since mode comes first
throw new ArgumentException(SR.Format(SR.Argument_InvalidFileModeAndAccessCombo, mode, access), nameof(access));
}
}
if ((access & FileAccess.Read) != 0 && mode == FileMode.Append)
throw new ArgumentException(SR.Argument_InvalidAppendMode, nameof(access));
string fullPath = Path.GetFullPath(path);
_path = fullPath;
_access = access;
_bufferLength = bufferSize;
if ((options & FileOptions.Asynchronous) != 0)
_useAsyncIO = true;
_fileHandle = OpenHandle(mode, share, options);
try
{
Init(mode, share);
}
catch
{
// If anything goes wrong while setting up the stream, make sure we deterministically dispose
// of the opened handle.
_fileHandle.Dispose();
_fileHandle = null;
throw;
}
}