C#/VB.NET 遍历目录/文件至列表
C#代码
/// <summary> /// 获取指定目录中的匹配项(文件或目录) /// </summary> /// <param name="dir">要搜索的目录</param> /// <param name="regexPattern">项名模式(正则)。null表示忽略模式匹配,返回所有项</param> /// <param name="depth">递归深度。负数表示不限,0表示仅顶级</param> /// <param name="throwEx">是否抛异常</param> /// <returns></returns> public static string[] GetFileSystemEntries(string dir, string regexPattern = null, int depth = 0, bool throwEx = false) { List<string> lst = new List<string>(); try { foreach (string item in Directory.GetFileSystemEntries(dir)) { try { if (regexPattern == null || Regex.IsMatch(Path.GetFileName(item), regexPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline)) { lst.Add(item); } //递归 if (depth != 0 && (File.GetAttributes(item) & FileAttributes.Directory) == FileAttributes.Directory) { lst.AddRange(GetFileSystemEntries(item, regexPattern, depth - 1, throwEx)); } } catch { if (throwEx) { throw; } } } } catch { if (throwEx) { throw; } } return lst.ToArray(); } /// <summary> /// 获取指定目录中的匹配文件 /// </summary> /// <param name="dir">要搜索的目录</param> /// <param name="regexPattern">文件名模式(正则)。null表示忽略模式匹配,返回所有文件</param> /// <param name="depth">递归深度。负数表示不限,0表示仅顶级</param> /// <param name="throwEx">是否抛异常</param> /// <returns></returns> public static string[] GetFiles(string dir, string regexPattern = null, int depth = 0, bool throwEx = false) { List<string> lst = new List<string>(); try { foreach (string item in Directory.GetFileSystemEntries(dir)) { try { bool isFile = (File.GetAttributes(item) & FileAttributes.Directory) != FileAttributes.Directory; if (isFile && (regexPattern == null || Regex.IsMatch(Path.GetFileName(item), regexPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline))) { lst.Add(item); } //递归 if (depth != 0 && !isFile) { lst.AddRange(GetFiles(item, regexPattern, depth - 1, throwEx)); } } catch { if (throwEx) { throw; } } } } catch { if (throwEx) { throw; } } return lst.ToArray(); } /// <summary> /// 获取指定目录中的匹配目录 /// </summary> /// <param name="dir">要搜索的目录</param> /// <param name="regexPattern">目fu录名模式(正则)。null表示忽略模式匹配,返回所有目录</param> /// <param name="depth">递归深度。负数表示不限,0表示仅顶级</param> /// <param name="throwEx">是否抛异常</param> /// <returns></returns> public static string[] GetDirectories(string dir, string regexPattern = null, int depth = 0, bool throwEx = false) { List<string> lst = new List<string>(); try { foreach (string item in Directory.GetDirectories(dir)) { try { if (regexPattern == null || Regex.IsMatch(Path.GetFileName(item), regexPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline)) { lst.Add(item); } //递归 if (depth != 0) { lst.AddRange(GetDirectories(item, regexPattern, depth - 1, throwEx)); } } catch { if (throwEx) { throw; } } } } catch { if (throwEx) { throw; } } return lst.ToArray(); }
VB.NET代码
''' <summary> ''' 获取指定目录中的匹配项(文件或目录) ''' </summary> ''' <param name="dir">要搜索的目录</param> ''' <param name="regexPattern">项名模式(正则)。null表示忽略模式匹配,返回所有项</param> ''' <param name="depth">递归深度。负数表示不限,0表示仅顶级</param> ''' <param name="throwEx">是否抛异常</param> ''' <returns></returns> Public Shared Function GetFileSystemEntries(dir As String, Optional regexPattern As String = Nothing, Optional depth As Integer = 0, Optional throwEx As Boolean = False) As String() Dim lst As New List(Of String)() Try For Each item As String In Directory.GetFileSystemEntries(dir) Try If regexPattern Is Nothing OrElse Regex.IsMatch(Path.GetFileName(item), regexPattern, RegexOptions.IgnoreCase Or RegexOptions.Multiline) Then lst.Add(item) End If '递归 If depth <> 0 AndAlso (File.GetAttributes(item) And FileAttributes.Directory) = FileAttributes.Directory Then lst.AddRange(GetFileSystemEntries(item, regexPattern, depth - 1, throwEx)) End If Catch If throwEx Then Throw End If End Try Next Catch If throwEx Then Throw End If End Try Return lst.ToArray() End Function ''' <summary> ''' 获取指定目录中的匹配文件 ''' </summary> ''' <param name="dir">要搜索的目录</param> ''' <param name="regexPattern">文件名模式(正则)。null表示忽略模式匹配,返回所有文件</param> ''' <param name="depth">递归深度。负数表示不限,0表示仅顶级</param> ''' <param name="throwEx">是否抛异常</param> ''' <returns></returns> Public Shared Function GetFiles(dir As String, Optional regexPattern As String = Nothing, Optional depth As Integer = 0, Optional throwEx As Boolean = False) As String() Dim lst As New List(Of String)() Try For Each item As String In Directory.GetFileSystemEntries(dir) Try Dim isFile As Boolean = (File.GetAttributes(item) And FileAttributes.Directory) <> FileAttributes.Directory If isFile AndAlso (regexPattern Is Nothing OrElse Regex.IsMatch(Path.GetFileName(item), regexPattern, RegexOptions.IgnoreCase Or RegexOptions.Multiline)) Then lst.Add(item) End If '递归 If depth <> 0 AndAlso Not isFile Then lst.AddRange(GetFiles(item, regexPattern, depth - 1, throwEx)) End If Catch If throwEx Then Throw End If End Try Next Catch If throwEx Then Throw End If End Try Return lst.ToArray() End Function ''' <summary> ''' 获取指定目录中的匹配目录 ''' </summary> ''' <param name="dir">要搜索的目录</param> ''' <param name="regexPattern">目fu录名模式(正则)。null表示忽略模式匹配,返回所有目录</param> ''' <param name="depth">递归深度。负数表示不限,0表示仅顶级</param> ''' <param name="throwEx">是否抛异常</param> ''' <returns></returns> Public Shared Function GetDirectories(dir As String, Optional regexPattern As String = Nothing, Optional depth As Integer = 0, Optional throwEx As Boolean = False) As String() Dim lst As New List(Of String)() Try For Each item As String In Directory.GetDirectories(dir) Try If regexPattern Is Nothing OrElse Regex.IsMatch(Path.GetFileName(item), regexPattern, RegexOptions.IgnoreCase Or RegexOptions.Multiline) Then lst.Add(item) End If '递归 If depth <> 0 Then lst.AddRange(GetDirectories(item, regexPattern, depth - 1, throwEx)) End If Catch If throwEx Then Throw End If End Try Next Catch If throwEx Then Throw End If End Try Return lst.ToArray() End Function