Example #1
0
 /// <summary>
 /// Creates the file information on the basis of the path and <see cref="Win32FindData"/>
 /// </summary>
 /// <param name="pathInfo">Full path to the file</param>
 /// <param name="win32FindData"><see cref="Win32FindData"/></param>
 internal FileDetail(PathInfo pathInfo, Win32FindData win32FindData)
 {
     PathInfo = pathInfo;
     CalculateSize(win32FindData);
 }
Example #2
0
 /// <summary>
 /// Calculates the size of the file from the handle
 /// </summary>
 /// <param name="win32FindData"></param>
 private void CalculateSize(Win32FindData win32FindData)
 {
     Bytes = win32FindData.CalculateBytes();
 }
Example #3
0
 internal static FileOrDirectory DetermineFileSystemEntry(Win32FindData findData)
 {
     return !ContainsFileAttribute(findData.dwFileAttributes, FileAttributes.Directory) ? FileOrDirectory.File : FileOrDirectory.Directory;
 }
Example #4
0
 /// <summary>
 /// Creates the file information on the basis of the path and <see cref="Win32FindData"/>
 /// </summary>
 /// <param name="fullName">Full path to the file</param>
 /// <param name="win32FindData"><see cref="Win32FindData"/></param>
 internal FileDetail(String fullName, Win32FindData win32FindData)
     : this(new PathInfo(fullName), win32FindData)
 {
 }
Example #5
0
 /// <summary>
 ///     Returns the handle by given path and finddata
 /// </summary>
 /// <param name="uncPath">Specified path</param>
 /// <param name="win32FindData">FindData to fill</param>
 /// <param name="win32Error">Win32Error Code. 0 on success</param>
 /// <returns><see cref="Win32FileHandle" /> of specified path</returns>
 static Win32FileHandle FindFirstFileManaged(String uncPath, Win32FindData win32FindData, out Int32 win32Error)
 {
     var handle = Win32SafeNativeMethods.FindFirstFile(uncPath, win32FindData);
     win32Error = Marshal.GetLastWin32Error();
     return handle;
 }
Example #6
0
        /// <summary>
        ///     Determined all files of a directory
        /// </summary>
        /// <param name="uncDirectoryPath">Path of the directory</param>
        /// <param name="pattern">Search pattern. Uses Win32 native filtering.</param>
        /// <param name="searchOption">
        ///     <see cref="SearchOption" />
        /// </param>
        /// <param name="enumerateOptions">The enumeration options for exception handling</param>
        /// <returns>Collection of files</returns>
        internal static IEnumerable<FileDetail> EnumerateFiles(String uncDirectoryPath, String pattern = "*", SearchOption searchOption = SearchOption.TopDirectoryOnly, SuppressExceptions enumerateOptions = SuppressExceptions.None)
        {
            // Match for start of search
            string currentPath = PathTools.Combine(uncDirectoryPath, pattern);

            // Find First file
            var win32FindData = new Win32FindData();
            int win32Error;
            using (var fileHandle = FindFirstFileManaged(currentPath, win32FindData, out win32Error))
            {
                // Take care of invalid handles
                if (fileHandle.IsInvalid && EnumerationHandleInvalidFileHandle(uncDirectoryPath, enumerateOptions, win32Error))
                {
                    yield return null;
                }

                // Treffer auswerten
                do
                {
                    // Ignore . and .. directories
                    if (IsSystemDirectoryEntry(win32FindData))
                    {
                        continue;
                    }

                    // Create hit for current search result
                    string resultPath = PathTools.Combine(uncDirectoryPath, win32FindData.cFileName);

                    // Check for Directory
                    if (!ContainsFileAttribute(win32FindData.dwFileAttributes, FileAttributes.Directory))
                    {
                        yield return new FileDetail(resultPath, win32FindData);
                    }
                    else
                    {
                        // SubFolders?!
                        if (searchOption == SearchOption.AllDirectories)
                        {
                            foreach (var match in EnumerateFiles(resultPath, pattern, searchOption, enumerateOptions))
                            {
                                yield return match;
                            }
                        }
                    }
                    // Create new FindData object for next result
                    win32FindData = new Win32FindData();
                } // Search for next entry
                while (Win32SafeNativeMethods.FindNextFile(fileHandle, win32FindData));
            }
        }
Example #7
0
        /// <summary>
        ///     Returns the <see cref="Win32FindData" /> from specified <paramref name="pathInfo" />
        /// </summary>
        /// <param name="pathInfo">Path to the file system entry</param>
        /// <returns>
        ///     <see cref="Win32FindData" />
        /// </returns>
        public static Win32FindData GetFindDataFromPath(PathInfo pathInfo)
        {
            var win32FindData = new Win32FindData();
            int win32Error;
            using (var fileHandle = FindFirstSafeFileHandle(pathInfo.FullNameUnc, win32FindData, out win32Error))
            {
                // Take care of invalid handles
                if (fileHandle.IsInvalid)
                {
                    NativeExceptionMapping(pathInfo.FullName, win32Error);
                }

                // Ignore . and .. directories
                if (!IsSystemDirectoryEntry(win32FindData))
                {
                    return win32FindData;
                }
            }

            throw new Exception("PathNotFound " + pathInfo.FullName);
        }
Example #8
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>
        /// <param name="win32FindData"></param>
        /// <param name="win32Error">Last error code. 0 if no error occurs</param>
        /// <returns>
        ///     <see cref="SafeFileHandle" />
        /// </returns>
        static Win32FileHandle FindFirstSafeFileHandle(string path, Win32FindData win32FindData, out Int32 win32Error)
        {
            var result = Win32SafeNativeMethods.FindFirstFile(path, win32FindData);
            win32Error = Marshal.GetLastWin32Error();

            return result;
        }
Example #9
0
        static Boolean IsSystemDirectoryEntry(Win32FindData win32FindData)
        {
            if (win32FindData.cFileName.Length >= 3)
            {
                return false;
            }

            return (win32FindData.cFileName == "." || win32FindData.cFileName == "..");
        }
Example #10
0
        /// <summary>
        ///     Gets the <see cref="Win32FindData" /> from the passed path.
        /// </summary>
        /// <param name="pathInfo">Path</param>
        /// <param name="pathFindData"><seealso cref="Win32FindData" />. Will be null if path does not exist.</param>
        /// <returns>true if path is valid and <see cref="Win32FindData" /> is set</returns>
        /// <remarks>
        ///     <see>
        ///         <cref>QuickIOCommon.NativeExceptionMapping</cref>
        ///     </see>
        ///     if invalid handle found.
        /// </remarks>
        public static bool TryGetFindDataFromPath(PathInfo pathInfo, out Win32FindData pathFindData)
        {
            var win32FindData = new Win32FindData();
            int win32Error;


            using (var fileHandle = FindFirstSafeFileHandle(pathInfo.FullNameUnc, win32FindData, out win32Error))
            {
                // Take care of invalid handles
                if (fileHandle.IsInvalid)
                {
                    NativeExceptionMapping(pathInfo.FullName, win32Error);
                }

                // Ignore . and .. directories
                if (!IsSystemDirectoryEntry(win32FindData))
                {
                    pathFindData = win32FindData;
                    return true;
                }
            }

            pathFindData = null;
            return false;
        }
Example #11
0
 public static bool IsSymLink(Win32FindData win32FindData)
 {
     return
         ((uint)win32FindData.dwFileAttributes & (uint)FileAttrFlags.FILE_ATTRIBUTE_REPARSE_POINT) == (uint)FileAttrFlags.FILE_ATTRIBUTE_REPARSE_POINT
     &&
         ((uint)win32FindData.dwReserved0 & SymLinkTag) == SymLinkTag;
 }