예제 #1
0
 /// <summary>
 /// Pass the file handle to the <see cref="System.IO.FileStream"/> constructor.
 /// The <see cref="System.IO.FileStream"/> will close the handle.
 /// </summary>
 public SafeFileHandle CreateHandle(
     CreationDisposition creationDisposition,
     FileAccess fileAccess,
     FileShare fileShare)
 {
     return(ZlpIOHelper.CreateFileHandle(_path, creationDisposition, fileAccess, fileShare));
 }
예제 #2
0
 /// <summary>
 /// Pass the file handle to the <see cref="System.IO.FileStream"/> constructor. 
 /// The <see cref="System.IO.FileStream"/> will close the handle.
 /// </summary>
 public SafeFileHandle CreateHandle(
     CreationDisposition creationDisposition,
     FileAccess fileAccess,
     FileShare fileShare)
 {
     return ZlpIOHelper.CreateFileHandle(FullName, creationDisposition, fileAccess, fileShare);
 }
예제 #3
0
        /// <summary>
        /// Pass the file handle to the <see cref="System.IO.FileStream"/> constructor. 
        /// The <see cref="System.IO.FileStream"/> will close the handle.
        /// </summary>
        public static SafeFileHandle CreateFileHandle(
			string filePath,
			CreationDisposition creationDisposition,
			FileAccess fileAccess,
			FileShare fileShare)
        {
            filePath = CheckAddLongPathPrefix(filePath);

            // Create a file with generic write access
            var fileHandle =
                PInvokeHelper.CreateFile(
                    filePath,
                    fileAccess,
                    fileShare,
                    IntPtr.Zero,
                    creationDisposition,
                    0,
                    IntPtr.Zero);

            // Check for errors.
            var lastWin32Error = Marshal.GetLastWin32Error();
            if (fileHandle.IsInvalid)
            {
                throw new Win32Exception(
                    lastWin32Error,
                    string.Format(
                        "Error {0} creating file handle for file path '{1}': {2}",
                        lastWin32Error,
                        filePath,
                        CheckAddDotEnd(new Win32Exception(lastWin32Error).Message)));
            }

            // Pass the file handle to FileStream. FileStream will close the handle.
            return fileHandle;
        }
예제 #4
0
        /// <summary>
        /// Pass the file handle to the <see cref="System.IO.FileStream"/> constructor. 
        /// The <see cref="System.IO.FileStream"/> will close the handle.
        /// </summary>
        public static SafeFileHandle CreateFileHandle(
            string filePath,
            CreationDisposition creationDisposition,
            FileAccess fileAccess,
            FileShare fileShare)
        {
            filePath = CheckAddLongPathPrefix(filePath);

            // Create a file with generic write access
            var fileHandle =
                PInvokeHelper.CreateFile(
                    filePath,
                    fileAccess,
                    fileShare,
                    IntPtr.Zero,
                    creationDisposition,
                    // Fix by Richard, 2015-04-14.
                    // See https://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx#DIRECTORIES,
                    // See http://stackoverflow.com/q/4998814/107625
                    FileAttributes.BackupSemantics,
                    IntPtr.Zero);

            // Check for errors.
            var lastWin32Error = Marshal.GetLastWin32Error();
            if (fileHandle.IsInvalid)
            {
                throw new Win32Exception(
                    lastWin32Error,
                    string.Format(
                        Resources.ErrorCreatingFileHandle,
                        lastWin32Error,
                        filePath,
                        CheckAddDotEnd(new Win32Exception(lastWin32Error).Message)));
            }

            // Pass the file handle to FileStream. FileStream will close the handle.
            return fileHandle;
        }