/// <summary> /// Determined metadata of directory /// </summary> /// <param name="uncDirectoryPath">Path of the directory</param> /// <param name="findData"> /// <see cref="Win32FindData" /> /// </param> /// <param name="enumerateOptions">The enumeration options for exception handling</param> /// <returns><see cref="DirectoryMetadata" /> started with the given directory</returns> internal static DirectoryMetadata EnumerateDirectoryMetadata(String uncDirectoryPath, Win32FindData findData, SuppressExceptions enumerateOptions) { // Results var subFiles = new List <FileMetadata>(); var subDirs = new List <DirectoryMetadata>(); // Match for start of search string currentPath = PathTools.Combine(uncDirectoryPath, "*"); // Find First file var win32FindData = new Win32FindData(); int win32Error; using (var fileHandle = FindFirstSafeFileHandle(currentPath, win32FindData, out win32Error)) { // Take care of invalid handles if (fileHandle.IsInvalid) { if (win32Error != ERROR_NO_MORE_FILES) { NativeExceptionMapping(uncDirectoryPath, win32Error); } if (EnumerationHandleInvalidFileHandle(uncDirectoryPath, enumerateOptions, win32Error)) { return(null); } } // Add any matching non-system results to the output do { // Ignore . and .. directories if (IsSystemDirectoryEntry(win32FindData)) { continue; } // Create hit for current search result var uncResultPath = PathTools.Combine(uncDirectoryPath, win32FindData.cFileName); // if it's a file, add to the collection if (!ContainsFileAttribute(win32FindData.dwFileAttributes, FileAttributes.Directory)) { subFiles.Add(new FileMetadata(uncResultPath, win32FindData)); } else { subDirs.Add(EnumerateDirectoryMetadata(uncResultPath, win32FindData, enumerateOptions)); } // Create new FindData object for next result win32FindData = new Win32FindData(); } // Search for next entry while (Win32SafeNativeMethods.FindNextFile(fileHandle, win32FindData)); } return(new DirectoryMetadata(uncDirectoryPath, findData, subDirs, subFiles)); }
/// <summary> /// Determined all subfolders of a directory /// </summary> /// <param name="pathInfo">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><see cref="DirectoryDetail" /> collection of subfolders</returns> internal static IEnumerable <DirectoryDetail> EnumerateDirectories(PathInfo pathInfo, String pattern = "*", SearchOption searchOption = SearchOption.TopDirectoryOnly, SuppressExceptions enumerateOptions = SuppressExceptions.None) { // Match for start of search string currentPath = PathTools.Combine(pathInfo.FullNameUnc, pattern); // Find First file var win32FindData = new Win32FindData(); int win32Error; using (var fileHandle = FindFirstSafeFileHandle(currentPath, win32FindData, out win32Error)) { // Take care of invalid handles if (fileHandle.IsInvalid) { if (win32Error != ERROR_NO_MORE_FILES) { NativeExceptionMapping(pathInfo.FullName, win32Error); } if (EnumerationHandleInvalidFileHandle(pathInfo.FullName, enumerateOptions, win32Error)) { yield return(null); } } do { // Ignore . and .. directories if (IsSystemDirectoryEntry(win32FindData)) { continue; } // Create hit for current search result string resultPath = PathTools.Combine(pathInfo.FullName, win32FindData.cFileName); // Check for Directory if (ContainsFileAttribute(win32FindData.dwFileAttributes, FileAttributes.Directory)) { yield return(new DirectoryDetail(new PathInfo(resultPath), win32FindData)); // SubFolders?! if (searchOption == SearchOption.AllDirectories) { foreach (var match in EnumerateDirectories(new PathInfo(resultPath, win32FindData.cFileName), 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)); } }
/// <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)); } }
/// <summary> /// Search Exection /// </summary> /// <param name="uncDirectoryPath">Start directory path</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> /// <param name="pathFormatReturn">Specifies the type of path to return.</param> /// <param name="filterType"> /// <see cref="FileOrDirectory" /> /// </param> /// <returns>Collection of path</returns> static IEnumerable <String> FindPaths(String uncDirectoryPath, String pattern = "*", SearchOption searchOption = SearchOption.TopDirectoryOnly, FileOrDirectory?filterType = null, SuppressExceptions enumerateOptions = SuppressExceptions.None, UncOrRegular pathFormatReturn = UncOrRegular.Regular) { // Result Container var results = new List <String>(); // Match for start of search string currentPath = PathTools.Combine(uncDirectoryPath, pattern); // Find First file var win32FindData = new Win32FindData(); int win32Error; using (var fileHandle = FindFirstSafeFileHandle(currentPath, win32FindData, out win32Error)) { // Take care of invalid handles if (fileHandle.IsInvalid && EnumerationHandleInvalidFileHandle(uncDirectoryPath, enumerateOptions, win32Error)) { return(new List <String>()); } do { // Ignore . and .. directories if (IsSystemDirectoryEntry(win32FindData)) { continue; } // Create hit for current search result string resultPath = PathTools.Combine(uncDirectoryPath, win32FindData.cFileName); // if it's a file, add to the collection if (!ContainsFileAttribute(win32FindData.dwFileAttributes, FileAttributes.Directory)) { if (filterType == null || ((FileOrDirectory)filterType == FileOrDirectory.File)) { // It's a file results.Add(FormatPathByType(pathFormatReturn, resultPath)); } } else { // It's a directory // Check for search searchFocus directories if (filterType != null && ((FileOrDirectory)filterType == FileOrDirectory.Directory)) { results.Add(FormatPathByType(pathFormatReturn, resultPath)); } // SubFolders?! if (searchOption == SearchOption.AllDirectories) { var r = new List <String>(FindPaths(resultPath, pattern, searchOption, filterType, enumerateOptions)); if (r.Count > 0) { results.AddRange(r); } } } // Create new FindData object for next result win32FindData = new Win32FindData(); } // Search for next entry while (Win32SafeNativeMethods.FindNextFile(fileHandle, win32FindData)); } // Return result; return(results); }