/// <summary> Enumerates the files in this collection. </summary> /// <param name="target"> The target to act on. </param> /// <param name="searchPattern"> A pattern specifying the search. </param> /// <returns> /// An enumerator that allows foreach to be used to process the files in this collection. /// </returns> public static IEnumerable <FileInfo> EnumerateFiles(this DirectoryInfo target, string searchPattern) { const int MAX_PATH = 248; const int MAX_FILENAME = 260; string searchPath = Path.Combine(target.FullName, searchPattern); if (searchPath.Length >= MAX_PATH) { yield break; } NativeWin32.WIN32_FIND_DATA findData; using (NativeWin32.SafeSearchHandle hFindFile = NativeWin32.FindFirstFile(searchPath, out findData)) { if (!hFindFile.IsInvalid) { do { if ((findData.dwFileAttributes & FileAttributes.Directory) == 0 && findData.cFileName != "." && findData.cFileName != "..") { string fileName = Path.Combine(target.FullName, findData.cFileName); if (fileName.Length >= MAX_FILENAME) { yield break; } else { yield return(new FileInfo(Path.Combine(target.FullName, findData.cFileName))); } } } while (NativeWin32.FindNextFile(hFindFile, out findData)); } } }
IEnumerable <FileInfo> EnumerateFiles(string directory, string searchPattern) { NativeWin32.WIN32_FIND_DATA findData; using (NativeWin32.SafeSearchHandle hFindFile = NativeWin32.FindFirstFile(Path.Combine(directory, searchPattern), out findData)) { if (!hFindFile.IsInvalid) { do { if ((findData.dwFileAttributes & FileAttributes.Directory) == 0 && findData.cFileName != "." && findData.cFileName != "..") { long len = (((long)findData.nFileSizeHigh) << 0x20) | findData.nFileSizeLow; yield return(new FileInfo { FileName = Path.Combine(directory, findData.cFileName), FileSize = len }); } } while (NativeWin32.FindNextFile(hFindFile, out findData)); } } }