Пример #1
0
        /// <summary>
        /// Returns the <see cref="SafeFileHandle"/> and fills <see cref="Win32FindData"/> from the passes path.
        /// </summary>
        /// <param name="path">Path to the file system entry</param>
        /// <returns><see cref="SafeFileHandle"/></returns>
        /// <exception cref="PathNotFoundException">This error is fired if the specified path or a part of them does not exist.</exception>
        private static SafeFileHandle CreateSafeFileHandle(string path)
        {
            Contract.Requires(!String.IsNullOrWhiteSpace(path));

            Contract.Ensures(Contract.Result <SafeFileHandle>() != null);

            return(Win32SafeNativeMethods.CreateFile(path, FileAccess.ReadWrite, FileShare.None, IntPtr.Zero, FileMode.Open, FileAttributes.Normal, IntPtr.Zero));
        }
Пример #2
0
        /// <summary>
        /// Opens a <see cref="FileStream"/> for access at the given path. Ensure stream is correctly disposed.
        /// </summary>
        public static FileStream OpenFileStream(PathInfo pathInfo, FileAccess fileAccess, FileMode fileOption = FileMode.Open, FileShare shareMode = FileShare.Read, Int32 buffer = 0)
        {
            var fileHandle = Win32SafeNativeMethods.CreateFile(pathInfo.FullNameUnc, fileAccess, shareMode, IntPtr.Zero, fileOption, 0, IntPtr.Zero);
            var win32Error = Marshal.GetLastWin32Error();

            if (fileHandle.IsInvalid)
            {
                NativeExceptionMapping(pathInfo.FullName, win32Error); // Throws an exception
            }

            return(buffer > 0 ? new FileStream(fileHandle, fileAccess, buffer) : new FileStream(fileHandle, fileAccess));
        }
Пример #3
0
        /// <summary>
        /// Creates a new file.
        /// </summary>
        /// <exception cref="PathAlreadyExistsException">The specified path already exists.</exception>
        /// <exception cref="PathNotFoundException">This error is fired if the specified path or a part of them does not exist.</exception>
        public static void CreateFile(String path, FileAccess fileAccess = FileAccess.Write, FileShare fileShare = FileShare.None, FileMode fileMode = FileMode.Create, FileAttributes fileAttributes = 0)
        {
            Contract.Requires(!String.IsNullOrWhiteSpace(path));

            using (var fileHandle = Win32SafeNativeMethods.CreateFile(path, fileAccess, fileShare, IntPtr.Zero, fileMode, fileAttributes, IntPtr.Zero))
            {
                var win32Error = Marshal.GetLastWin32Error();
                if (fileHandle.IsInvalid)
                {
                    Win32ErrorCodes.NativeExceptionMapping(path, win32Error);
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Opens a <see cref="FileStream"/>
        /// </summary>
        public static FileStream OpenFileStream(string path, FileAccess fileAccess, FileMode fileOption = FileMode.Open, FileShare shareMode = FileShare.Read, Int32 buffer = 0)
        {
            Contract.Requires(!String.IsNullOrWhiteSpace(path));
            Contract.Ensures(Contract.Result <FileStream>() != null);

            var fileHandle = Win32SafeNativeMethods.CreateFile(path, fileAccess, shareMode, IntPtr.Zero, fileOption, 0, IntPtr.Zero);
            var win32Error = Marshal.GetLastWin32Error();

            if (fileHandle.IsInvalid)
            {
                Win32ErrorCodes.NativeExceptionMapping(path, win32Error);   // Throws an exception
            }

            return(buffer > 0 ? new FileStream(fileHandle, fileAccess, buffer) : new FileStream(fileHandle, fileAccess));
        }
Пример #5
0
 private static SafeFileHandle getFileHandle(string path)
 {
     return(Win32SafeNativeMethods.CreateFile(path, GenericReadAccess, ShareModeAll, IntPtr.Zero, OpenExisting,
                                              FileFlagsForOpenReparsePointAndBackupSemantics, IntPtr.Zero));
 }