/* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ using Microsoft.Win32.SafeHandles; using System.IO; using System.Security; using System.Security.AccessControl; using FileStream = System.IO.FileStream; using System.Diagnostics.CodeAnalysis; namespace Alphaleonis.Win32.Filesystem { partial class File { #region Using FileAccess /// Opens a on the specified path with read/write access. /// The file to open. /// A value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten. /// A opened in the specified mode and path, with read/write access and not shared. [SecurityCritical] public static FileStream Open(string path, FileMode mode) { return OpenCore(null, path, mode, mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite, FileShare.None, ExtendedFileAttributes.Normal, null, null, PathFormat.RelativePath); } /// Opens a on the specified path, with the specified mode and access. /// The file to open. /// A value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten. /// A value that specifies the operations that can be performed on the file. /// An unshared that provides access to the specified file, with the specified mode and access. [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileAccess access) { return OpenCore(null, path, mode, access, FileShare.None, ExtendedFileAttributes.Normal, null, null, PathFormat.RelativePath); } /// Opens a on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option. /// The file to open. /// A value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten. /// A value that specifies the operations that can be performed on the file. /// A value specifying the type of access other threads have to the file. /// A on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option. [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share) { return OpenCore(null, path, mode, access, share, ExtendedFileAttributes.Normal, null, null, PathFormat.RelativePath); } /// [AlphaFS] Opens a on the specified path with read/write access. /// The file to open. /// /// A value that specifies whether a file is created if one does not exist, and determines whether the contents /// of existing files are retained or overwritten. /// /// Indicates the format of the path parameter(s). /// A opened in the specified mode and path, with read/write access and not shared. [SecurityCritical] public static FileStream Open(string path, FileMode mode, PathFormat pathFormat) { return OpenCore(null, path, mode, mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite, FileShare.None, ExtendedFileAttributes.Normal, null, null, pathFormat); } /// [AlphaFS] Opens a on the specified path, with the specified mode and access. /// The file to open. /// /// A value that specifies whether a file is created if one does not exist, and determines whether the contents /// of existing files are retained or overwritten. /// /// A value that specifies the operations that can be performed on the file. /// Indicates the format of the path parameter(s). /// /// An unshared that provides access to the specified file, with the specified mode and access. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileAccess access, PathFormat pathFormat) { return OpenCore(null, path, mode, access, FileShare.None, ExtendedFileAttributes.Normal, null, null, pathFormat); } /// [AlphaFS] Opens a on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option. /// The file to open. /// /// A value that specifies whether a file is created if one does not exist, and determines whether the contents /// of existing files are retained or overwritten. /// /// A value that specifies the operations that can be performed on the file. /// A value specifying the type of access other threads have to the file. /// Indicates the format of the path parameter(s). /// /// A on the specified path, having the specified mode with read, write, or read/write access and the /// specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, PathFormat pathFormat) { return OpenCore(null, path, mode, access, share, ExtendedFileAttributes.Normal, null, null, pathFormat); } /// [AlphaFS] Opens a on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option. /// The file to open. /// /// A value that specifies whether a file is created if one does not exist, and determines whether the contents /// of existing files are retained or overwritten. /// /// A value that specifies the operations that can be performed on the file. /// A value specifying the type of access other threads have to the file. /// The extended attributes. /// Indicates the format of the path parameter(s). /// /// A on the specified path, having the specified mode with read, write, or read/write access and the /// specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, ExtendedFileAttributes extendedAttributes, PathFormat pathFormat) { return OpenCore(null, path, mode, access, share, extendedAttributes, null, null, pathFormat); } // New below /// [AlphaFS] Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The default buffer size is 4096. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize) { return OpenCore(null, path, mode, access, share, ExtendedFileAttributes.Normal, bufferSize, null, PathFormat.RelativePath); } /// [AlphaFS] Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// Specifies whether to use asynchronous I/O or synchronous I/O. However, note that the /// underlying operating system might not support asynchronous I/O, so when specifying true, the handle might be /// opened synchronously depending on the platform. When opened asynchronously, the BeginRead and BeginWrite methods /// perform better on large reads or writes, but they might be much slower for small reads or writes. If the /// application is designed to take advantage of asynchronous I/O, set the useAsync parameter to true. Using /// asynchronous I/O correctly can speed up applications by as much as a factor of 10, but using it without /// redesigning the application for asynchronous I/O can decrease performance by as much as a factor of 10. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync) { return OpenCore(null, path, mode, access, share, ExtendedFileAttributes.Normal | (useAsync ? ExtendedFileAttributes.Overlapped : ExtendedFileAttributes.Normal), bufferSize, null, PathFormat.RelativePath); } /// [AlphaFS] Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// A value that specifies additional file options. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options) { return OpenCore(null, path, mode, access, share, (ExtendedFileAttributes) options, bufferSize, null, PathFormat.RelativePath); } /// [AlphaFS] Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// The extended attributes specifying additional options. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes) { return OpenCore(null, path, mode, access, share, extendedAttributes, bufferSize, null, PathFormat.RelativePath); } /// [AlphaFS] Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// Indicates the format of the path parameter. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, PathFormat pathFormat) { return OpenCore(null, path, mode, access, share, ExtendedFileAttributes.Normal, bufferSize, null, pathFormat); } /// [AlphaFS] Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// Specifies whether to use asynchronous I/O or synchronous I/O. However, note that the /// underlying operating system might not support asynchronous I/O, so when specifying true, the handle might be /// opened synchronously depending on the platform. When opened asynchronously, the BeginRead and BeginWrite methods /// perform better on large reads or writes, but they might be much slower for small reads or writes. If the /// application is designed to take advantage of asynchronous I/O, set the useAsync parameter to true. Using /// asynchronous I/O correctly can speed up applications by as much as a factor of 10, but using it without /// redesigning the application for asynchronous I/O can decrease performance by as much as a factor of 10. /// Indicates the format of the path parameter. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync, PathFormat pathFormat) { return OpenCore(null, path, mode, access, share, ExtendedFileAttributes.Normal | (useAsync ? ExtendedFileAttributes.Overlapped : ExtendedFileAttributes.Normal), bufferSize, null, pathFormat); } /// [AlphaFS] Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// A value that specifies additional file options. /// Indicates the format of the path parameter. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, PathFormat pathFormat) { return OpenCore(null, path, mode, access, share, (ExtendedFileAttributes) options, bufferSize, null, pathFormat); } /// [AlphaFS] Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// The extended attributes specifying additional options. /// Indicates the format of the path parameter. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes, PathFormat pathFormat) { return OpenCore(null, path, mode, access, share, extendedAttributes, bufferSize, null, pathFormat); } #endregion // Using FileAccess #region Using FileSystemRights /// [AlphaFS] Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// A value that specifies additional file options. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options) { return OpenCore(null, path, mode, rights, share, (ExtendedFileAttributes)options, bufferSize, null, PathFormat.RelativePath); } /// [AlphaFS] Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// Extended attributes specifying additional options. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes) { return OpenCore(null, path, mode, rights, share, extendedAttributes, bufferSize, null, PathFormat.RelativePath); } /// [AlphaFS] Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// A value that specifies additional file options. /// A value that determines the access control and audit security for the file. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity security) { return OpenCore(null, path, mode, rights, share, (ExtendedFileAttributes)options, bufferSize, security, PathFormat.RelativePath); } /// [AlphaFS] Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// Extended attributes specifying additional options. /// A value that determines the access control and audit security for the file. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes, FileSecurity security) { return OpenCore(null, path, mode, rights, share, extendedAttributes, bufferSize, security, PathFormat.RelativePath); } /// [AlphaFS] Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// A value that specifies additional file options. /// Indicates the format of the path parameter. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, PathFormat pathFormat) { return OpenCore(null, path, mode, rights, share, (ExtendedFileAttributes) options, bufferSize, null, pathFormat); } /// [AlphaFS] Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// Extended attributes specifying additional options. /// Indicates the format of the path parameter. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes, PathFormat pathFormat) { return OpenCore(null, path, mode, rights, share, extendedAttributes, bufferSize, null, pathFormat); } /// [AlphaFS] Opens a on the specified path using the specified creation mode, access rights and sharing permission, the buffer size, additional file options, access control and audit security. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// A value that specifies additional file options. /// A value that determines the access control and audit security for the file. /// Indicates the format of the path parameter. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity security, PathFormat pathFormat) { return OpenCore(null, path, mode, rights, share, (ExtendedFileAttributes) options, bufferSize, security, pathFormat); } /// [AlphaFS] Opens a on the specified path using the specified creation mode, access rights and sharing permission, the buffer size, additional file options, access control and audit security. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// Extended attributes specifying additional options. /// A value that determines the access control and audit security for the file. /// Indicates the format of the path parameter. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream Open(string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes, FileSecurity security, PathFormat pathFormat) { return OpenCore(null, path, mode, rights, share, extendedAttributes, bufferSize, security, pathFormat); } #endregion // Using FileSystemRights #region Transactional /// [AlphaFS] (Transacted) Opens a on the specified path with read/write access. /// The transaction. /// The file to open. /// /// A value that specifies whether a file is created if one does not exist, and determines whether the contents /// of existing files are retained or overwritten. /// /// A opened in the specified mode and path, with read/write access and not shared. [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode) { return OpenCore(transaction, path, mode, mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite, FileShare.None, ExtendedFileAttributes.Normal, null, null, PathFormat.RelativePath); } /// [AlphaFS] (Transacted) Opens a on the specified path with read/write access. /// The transaction. /// The file to open. /// /// A value that specifies whether a file is created if one does not exist, and determines whether the contents /// of existing files are retained or overwritten. /// /// Indicates the format of the path parameter(s). /// A opened in the specified mode and path, with read/write access and not shared. [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, PathFormat pathFormat) { return OpenCore(transaction, path, mode, mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite, FileShare.None, ExtendedFileAttributes.Normal, null, null, pathFormat); } #region Using FileAccess /// [AlphaFS] (Transacted) Opens a on the specified path, with the specified mode and access. /// The transaction. /// The file to open. /// /// A value that specifies whether a file is created if one does not exist, and determines whether the contents /// of existing files are retained or overwritten. /// /// A value that specifies the operations that can be performed on the file. /// /// An unshared that provides access to the specified file, with the specified mode and access. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access) { return OpenCore(transaction, path, mode, access, FileShare.None, ExtendedFileAttributes.Normal, null, null, PathFormat.RelativePath); } /// [AlphaFS] (Transacted) Opens a on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option. /// The transaction. /// The file to open. /// /// A value that specifies whether a file is created if one does not exist, and determines whether the contents /// of existing files are retained or overwritten. /// /// A value that specifies the operations that can be performed on the file. /// A value specifying the type of access other threads have to the file. /// /// A on the specified path, having the specified mode with read, write, or read/write access and the /// specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share) { return OpenCore(transaction, path, mode, access, share, ExtendedFileAttributes.Normal, null, null, PathFormat.RelativePath); } /// [AlphaFS] (Transacted) Opens a on the specified path, with the specified mode and access. /// The transaction. /// The file to open. /// /// A value that specifies whether a file is created if one does not exist, and determines whether the contents /// of existing files are retained or overwritten. /// /// A value that specifies the operations that can be performed on the file. /// Indicates the format of the path parameter(s). /// /// An unshared that provides access to the specified file, with the specified mode and access. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, PathFormat pathFormat) { return OpenCore(transaction, path, mode, access, FileShare.None, ExtendedFileAttributes.Normal, null, null, pathFormat); } /// [AlphaFS] (Transacted) Opens a on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option. /// The transaction. /// The file to open. /// A value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten. /// A value that specifies the operations that can be performed on the file. /// A value specifying the type of access other threads have to the file. /// Indicates the format of the path parameter(s). /// A on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option. [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, PathFormat pathFormat) { return OpenCore(transaction, path, mode, access, share, ExtendedFileAttributes.Normal, null, null, pathFormat); } /// [AlphaFS] (Transacted) Opens a on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option. /// The transaction. /// The file to open. /// /// A value that specifies whether a file is created if one does not exist, and determines whether the contents /// of existing files are retained or overwritten. /// /// A value that specifies the operations that can be performed on the file. /// A value specifying the type of access other threads have to the file. /// The extended attributes. /// Indicates the format of the path parameter(s). /// /// A on the specified path, having the specified mode with read, write, or read/write access and the /// specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, ExtendedFileAttributes extendedAttributes, PathFormat pathFormat) { return OpenCore(transaction, path, mode, access, share, extendedAttributes, null, null, pathFormat); } // New below /// [AlphaFS] (Transacted) Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The transaction. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize) { return OpenCore(transaction, path, mode, access, share, ExtendedFileAttributes.Normal, bufferSize, null, PathFormat.RelativePath); } /// [AlphaFS] (Transacted) Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The transaction. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// Specifies whether to use asynchronous I/O or synchronous I/O. However, note that the /// underlying operating system might not support asynchronous I/O, so when specifying true, the handle might be /// opened synchronously depending on the platform. When opened asynchronously, the BeginRead and BeginWrite methods /// perform better on large reads or writes, but they might be much slower for small reads or writes. If the /// application is designed to take advantage of asynchronous I/O, set the useAsync parameter to true. Using /// asynchronous I/O correctly can speed up applications by as much as a factor of 10, but using it without /// redesigning the application for asynchronous I/O can decrease performance by as much as a factor of 10. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync) { return OpenCore(transaction, path, mode, access, share, ExtendedFileAttributes.Normal | (useAsync ? ExtendedFileAttributes.Overlapped : ExtendedFileAttributes.Normal), bufferSize, null, PathFormat.RelativePath); } /// [AlphaFS] (Transacted) Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The transaction. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// A value that specifies additional file options. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options) { return OpenCore(transaction, path, mode, access, share, (ExtendedFileAttributes) options, bufferSize, null, PathFormat.RelativePath); } /// [AlphaFS] (Transacted) Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The transaction. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// The extended attributes specifying additional options. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes) { return OpenCore(transaction, path, mode, access, share, extendedAttributes, bufferSize, null, PathFormat.RelativePath); } /// [AlphaFS] (Transacted) Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The transaction. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// Indicates the format of the path parameter. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, PathFormat pathFormat) { return OpenCore(transaction, path, mode, access, share, ExtendedFileAttributes.Normal, bufferSize, null, pathFormat); } /// [AlphaFS] (Transacted) Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The transaction. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// Specifies whether to use asynchronous I/O or synchronous I/O. However, note that the /// underlying operating system might not support asynchronous I/O, so when specifying true, the handle might be /// opened synchronously depending on the platform. When opened asynchronously, the BeginRead and BeginWrite methods /// perform better on large reads or writes, but they might be much slower for small reads or writes. If the /// application is designed to take advantage of asynchronous I/O, set the useAsync parameter to true. Using /// asynchronous I/O correctly can speed up applications by as much as a factor of 10, but using it without /// redesigning the application for asynchronous I/O can decrease performance by as much as a factor of 10. /// Indicates the format of the path parameter. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync, PathFormat pathFormat) { return OpenCore(transaction, path, mode, access, share, ExtendedFileAttributes.Normal | (useAsync ? ExtendedFileAttributes.Overlapped : ExtendedFileAttributes.Normal), bufferSize, null, pathFormat); } /// [AlphaFS] (Transacted) Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The transaction. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// A value that specifies additional file options. /// Indicates the format of the path parameter. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, PathFormat pathFormat) { return OpenCore(transaction, path, mode, access, share, (ExtendedFileAttributes) options, bufferSize, null, pathFormat); } /// [AlphaFS] (Transacted) Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The transaction. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// The extended attributes specifying additional options. /// Indicates the format of the path parameter. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes, PathFormat pathFormat) { return OpenCore(transaction, path, mode, access, share, extendedAttributes, bufferSize, null, pathFormat); } #endregion // Using FileAccess #region Using FileSystemRights /// [AlphaFS] (Transacted) Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The transaction. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// A value that specifies additional file options. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options) { return OpenCore(transaction, path, mode, rights, share, (ExtendedFileAttributes) options, bufferSize, null, PathFormat.RelativePath); } /// [AlphaFS] (Transacted) Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The transaction. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// Extended attributes specifying additional options. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes) { return OpenCore(transaction, path, mode, rights, share, extendedAttributes, bufferSize, null, PathFormat.RelativePath); } /// [AlphaFS] (Transacted) Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The transaction. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// A value that specifies additional file options. /// A value that determines the access control and audit security for the file. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity security) { return OpenCore(transaction, path, mode, rights, share, (ExtendedFileAttributes) options, bufferSize, security, PathFormat.RelativePath); } /// [AlphaFS] (Transacted) Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The transaction. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// Extended attributes specifying additional options. /// A value that determines the access control and audit security for the file. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes, FileSecurity security) { return OpenCore(transaction, path, mode, rights, share, extendedAttributes, bufferSize, security, PathFormat.RelativePath); } /// [AlphaFS] (Transacted) Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The transaction. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// A value that specifies additional file options. /// Indicates the format of the path parameter. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, PathFormat pathFormat) { return OpenCore(transaction, path, mode, rights, share, (ExtendedFileAttributes) options, bufferSize, null, pathFormat); } /// [AlphaFS] (Transacted) Opens a on the specified path using the specified creation mode, read/write and sharing permission, and buffer size. /// The transaction. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// Extended attributes specifying additional options. /// Indicates the format of the path parameter. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes, PathFormat pathFormat) { return OpenCore(transaction, path, mode, rights, share, extendedAttributes, bufferSize, null, pathFormat); } /// [AlphaFS] (Transacted) Opens a on the specified path using the specified creation mode, access rights and sharing permission, the buffer size, additional file options, access control and audit security. /// The transaction. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// A value that specifies additional file options. /// A value that determines the access control and audit security for the file. /// Indicates the format of the path parameter. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, FileOptions options, FileSecurity security, PathFormat pathFormat) { return OpenCore(transaction, path, mode, rights, share, (ExtendedFileAttributes) options, bufferSize, security, pathFormat); } /// [AlphaFS] (Transacted) Opens a on the specified path using the specified creation mode, access rights and sharing permission, the buffer size, additional file options, access control and audit security. /// The transaction. /// The file to open. /// A constant that determines how to open or create the file. /// A value that specifies the operations that can be performed on the /// file. /// A constant that determines how the file will be shared by processes. /// A positive value greater than 0 indicating the buffer size. The /// default buffer size is 4096. /// Extended attributes specifying additional options. /// A value that determines the access control and audit security for the file. /// Indicates the format of the path parameter. /// /// A on the specified path, having the specified mode with read, write, or read/write /// access and the specified sharing option. /// [SecurityCritical] public static FileStream OpenTransacted(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, int bufferSize, ExtendedFileAttributes extendedAttributes, FileSecurity security, PathFormat pathFormat) { return OpenCore(transaction, path, mode, rights, share, extendedAttributes, bufferSize, security, pathFormat); } #endregion // Using FileSystemRights #endregion // Transacted #region Internal Methods /// [AlphaFS] Opens a on the specified path, having the specified mode with read, write, or read/write access, the specified sharing option and additional options specified. /// The transaction. /// The file to open. /// A value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten. /// A value that specifies the operations that can be performed on the file. /// A value specifying the type of access other threads have to the file. /// Advanced options for this file. /// A positive value greater than 0 indicating the buffer size. The default buffer size is 4096. /// The security. /// Indicates the format of the path parameter(s). /// /// A instance on the specified path, having the specified mode with /// read, write, or read/write access and the specified sharing option. /// internal static FileStream OpenCore(KernelTransaction transaction, string path, FileMode mode, FileAccess access, FileShare share, ExtendedFileAttributes attributes, int? bufferSize, FileSecurity security, PathFormat pathFormat) { var rights = access == FileAccess.Read ? FileSystemRights.Read : (access == FileAccess.Write ? FileSystemRights.Write : FileSystemRights.Read | FileSystemRights.Write); return OpenCore(transaction, path, mode, rights, share, attributes, bufferSize, security, pathFormat); } /// Opens a on the specified path, having the specified mode with read, write, or read/write access, the specified sharing option and additional options specified. /// The transaction. /// The file to open. /// A value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten. /// A value that specifies whether a file is created if one does not exist, and determines whether the contents of existing files are retained or overwritten along with additional options. /// A value specifying the type of access other threads have to the file. /// Advanced options for this file. /// A positive value greater than 0 indicating the buffer size. The default buffer size is 4096. /// The security. /// Indicates the format of the path parameter(s). /// /// A instance on the specified path, having the specified mode with /// read, write, or read/write access and the specified sharing option. /// [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")] internal static FileStream OpenCore(KernelTransaction transaction, string path, FileMode mode, FileSystemRights rights, FileShare share, ExtendedFileAttributes attributes, int? bufferSize, FileSecurity security, PathFormat pathFormat) { var access = ((rights & FileSystemRights.ReadData) != 0 ? FileAccess.Read : 0) | ((rights & FileSystemRights.WriteData) != 0 || (rights & FileSystemRights.AppendData) != 0 ? FileAccess.Write : 0); SafeFileHandle safeHandle = null; try { safeHandle = CreateFileCore(transaction, path, attributes, security, mode, rights, share, true, pathFormat); return new FileStream(safeHandle, access, bufferSize ?? NativeMethods.DefaultFileBufferSize, (attributes & ExtendedFileAttributes.Overlapped) != 0); } catch { if (safeHandle != null) safeHandle.Dispose(); throw; } } #endregion // Internal Methods } }