public static List <string> GetFiles(string path, string searchPattern = "*", System.IO.SearchOption searchOption = System.IO.SearchOption.TopDirectoryOnly) { searchPattern = searchPattern ?? "*"; var files = new List <string>(); var dirs = new List <string> { GetWin32LongPath(path) }; if (searchOption == System.IO.SearchOption.AllDirectories) { //Add all the subpaths dirs.AddRange(Win32LongPathDirectory.GetDirectories(path, null, System.IO.SearchOption.AllDirectories)); } foreach (var dir in dirs) { Win32FileSystem.WIN32_FIND_DATA findData; IntPtr findHandle = Win32FileSystem.FindFirstFile(System.IO.Path.Combine(GetWin32LongPath(dir), searchPattern), out findData); try { if (findHandle != new IntPtr(-1)) { do { if ((findData.dwFileAttributes & System.IO.FileAttributes.Directory) == 0) { string filename = System.IO.Path.Combine(dir, findData.cFileName); files.Add(GetCleanPath(filename)); } } while (Win32FileSystem.FindNextFile(findHandle, out findData)); Win32FileSystem.FindClose(findHandle); } } catch (Exception) { Win32FileSystem.FindClose(findHandle); throw; } } return(files); }
private static void InternalGetDirectories(string path, string searchPattern, System.IO.SearchOption searchOption, ref List <string> dirs) { Win32FileSystem.WIN32_FIND_DATA findData; IntPtr findHandle = Win32FileSystem.FindFirstFile(System.IO.Path.Combine(GetWin32LongPath(path), searchPattern), out findData); try { if (findHandle != new IntPtr(-1)) { do { if ((findData.dwFileAttributes & System.IO.FileAttributes.Directory) != 0) { if (findData.cFileName != "." && findData.cFileName != "..") { string subdirectory = System.IO.Path.Combine(path, findData.cFileName); dirs.Add(GetCleanPath(subdirectory)); if (searchOption == System.IO.SearchOption.AllDirectories) { InternalGetDirectories(subdirectory, searchPattern, searchOption, ref dirs); } } } } while (Win32FileSystem.FindNextFile(findHandle, out findData)); Win32FileSystem.FindClose(findHandle); } else { //ThrowWin32Exception(); } } catch (Exception) { Win32FileSystem.FindClose(findHandle); throw; } }