Пример #1
0
 public static extern SafeFileHandle CreateFile(
     string lpFileName,
     NativeMethods.EFileAccess dwDesiredAccess,
     NativeMethods.EFileShare dwShareMode,
     IntPtr lpSecurityAttributes,
     NativeMethods.ECreationDisposition dwCreationDisposition,
     NativeMethods.EFileAttributes dwFlagsAndAttributes,
     IntPtr hTemplateFile);
        /// <summary>
        /// Creates or overwrites the file at the specified path.
        /// </summary>
        /// <param name="path">The path and name of the file to create. Supports long file paths.</param>
        /// <returns>A <see cref="System.IO.Stream"/> that provides read/write access to the file specified in path.</returns>
        public override System.IO.Stream CreateFile(Path path)
        {
            if (path.IsEmpty)
            {
                throw new ArgumentNullException("path");
            }

            NativeMethods.EFileAccess          fileAccess          = NativeMethods.EFileAccess.GenericWrite | NativeMethods.EFileAccess.GenericRead;
            NativeMethods.EFileShare           fileShareMode       = NativeMethods.EFileShare.None;//exclusive
            NativeMethods.ECreationDisposition creationDisposition = NativeMethods.ECreationDisposition.CreateAlways;
            SafeFileHandle hFile = NativeMethods.CreateFile(path.WithWin32LongPathPrefix(), fileAccess, fileShareMode, IntPtr.Zero, creationDisposition, NativeMethods.EFileAttributes.Normal, IntPtr.Zero);

            if (hFile.IsInvalid)
            {
                throw CreateWin32LastErrorException("Error creating file at path '{0}'.", path);
            }
            return(new System.IO.FileStream(hFile, System.IO.FileAccess.ReadWrite));
        }