public static extern SafeFileHandle CreateFile( string filename, NativeFileAccess access, FileShare share, IntPtr security, FileMode mode, NativeFileFlags flags, IntPtr template );
public static SafeFileHandle SafeCreateFile(string path, NativeFileAccess access, FileShare share, IntPtr security, FileMode mode, NativeFileFlags flags, IntPtr template) { SafeFileHandle result = CreateFile(path, access, share, security, mode, flags, template); if (!result.IsInvalid && 1 != GetFileType(result)) { result.Dispose(); throw new NotSupportedException(string.Format(Resources.Culture, Resources.Error_NonFile, path)); } return(result); }
/// <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), bufferSize, null); } #if NET35 FileIOPermissionAccess permAccess = CalculateAccess(mode, access); new FileIOPermission(permAccess, FilePath).Demand(); #endif NativeFileFlags flags = useAsync ? NativeFileFlags.Overlapped : 0; var handle = Kernel.SafeCreateFile(FullPath, access.ToNative(), share, IntPtr.Zero, mode, flags, IntPtr.Zero); if (handle.IsInvalid) { Kernel.ThrowLastIOError(FullPath); } return(new FileStream(handle, access, bufferSize, useAsync)); }
private static extern SafeFileHandle CreateFile( string name, NativeFileAccess access, FileShare share, IntPtr security, FileMode mode, NativeFileFlags flags, IntPtr template);
public static SafeFileHandle SafeCreateFile(string path, NativeFileAccess access, FileShare share, IntPtr security, FileMode mode, NativeFileFlags flags, IntPtr template) { SafeFileHandle result = CreateFile(path, access, share, security, mode, flags, template); if (!result.IsInvalid && 1 != GetFileType(result)) { result.Dispose(); throw new NotSupportedException(string.Format(Resources.Culture, Resources.Error_NonFile, path)); } return result; }
internal static SafeFileHandle SafeCreateFile(string path, NativeFileAccess access, FileShare share, IntPtr security, FileMode mode, NativeFileFlags flags, IntPtr template) { var result = Kernel32.CreateFile(path, access, share, security, mode, flags, template); if (!result.IsInvalid && 1 != Kernel32.GetFileType(result)) { result.Dispose(); throw new NotSupportedException($"The specified file name '{path}' is not a disk-based file."); } return(result); }
public static SafeFileHandle SafeCreateFile(string path, NativeFileAccess access, FileShare share, IntPtr security, FileMode mode, NativeFileFlags flags, IntPtr template) { SafeFileHandle result = CreateFile(path, access, share, security, mode, flags, template); if (!result.IsInvalid && 1 != GetFileType(result)) { result.Dispose(); throw new NotSupportedException(string.Format("The specified file name '{0}' is not a disk-based file.", path)); } return(result); }