private FileObject.FileMemoryStream OpenStreamInternal(AbsolutePath path, FileAccess accessMode, FileMode mode, FileShare share) { if (mode == FileMode.CreateNew && accessMode == FileAccess.Read) { throw new ArgumentException("FileMode.CreateNew and FileAccess.Read is an invalid combination."); } lock (_drives) { FileObject fileObj = FindFileObjectAndParent(path, out var parent); if (parent == null) { return(null); } switch (mode) { case FileMode.Create: if (fileObj != null) { fileObj.DeleteLink(path, false); parent.Children.Remove(path.FileName); } fileObj = new FileObject(path, this, new byte[] {}); parent.Children[path.FileName] = fileObj; break; case FileMode.Open: if (fileObj == null) { return(null); } break; case FileMode.OpenOrCreate: if (fileObj == null) { fileObj = new FileObject(path, this, new byte[] {}); parent.Children[path.FileName] = fileObj; } break; case FileMode.CreateNew: if (fileObj != null) { unchecked { throw new IOException("File already exists", new IOException("File exists", (int)Hresult.FileExists)); } } fileObj = new FileObject(path, this, new byte[] {}); parent.Children[path.FileName] = fileObj; break; default: throw new NotImplementedException($"Mode '{mode}' is not supported."); } var file = fileObj.Open(path, accessMode, share); if (accessMode.HasFlag(FileAccess.Write)) { _currentStatistics.FileOpensForWrite++; } else { _currentStatistics.FileOpensForRead++; } return(file); } }