Skip to main content
 首页 » 编程设计

c#之目录枚举以跳过文件夹

2024年02月13日22JeffreyZhao

我有一段可爱的代码,对我来说非常出色,只需获取我需要的所有文件以进行多次搜索等。

public static IEnumerable<string> GetFiles(string path, string searchPatternExpression = "", SearchOption searchOption = SearchOption.AllDirectories) 
{ 
    Regex reSearchPattern = new Regex(searchPatternExpression); 
    return Directory.EnumerateFiles(path, "*", searchOption) 
                    .Where(file => reSearchPattern.IsMatch(System.IO.Path.GetExtension(file))); 
} 

但是有一个文件夹我不需要报告显示在我的目录之一中。我们将文件夹称为“Narnia”。我知道有一个 Directory.Skip 但我不完全确定如何使用它。

调用 GetFiles() 的命令;在下面。它只是将返回的列表写到一个 txt 文件中。我想知道我可以从那里过滤它吗?

internal static void GetFilesPassThrough(string SearchRoot, string extensions, string savepath) //GetFiles Regex Thread Start.  
{ 
    try 
    { 
        foreach (string file in GetFiles(SearchRoot, extensions)) 
            using (StreamWriter writer = new StreamWriter(savepath, true)) 
            { 
                writer.WriteLine(file); 
            } 
    } 
    catch (Exception ex) 
    { 
        MessageBox.Show(ex.Message + savepath); 
    } 
} 

附加信息

按照 James 的要求,我将提供更深入的代码调用方式。

*按下调用 GetFilesPassThrough(SearchDirectory, Extensions, savepath) 的按钮 * 扩展名是我需要从目录中报告的文件,.PDF、.txt、.xls 等 * 正如您在上面的 GetFilesPassThrough 代码中看到的,它尝试使用 GetFile() 并返回列表中回调的每个字符串。

*按钮 > GetFilesPassThrough> 使用获取文件创建列表> 并写入文本文件 *

请您参考如下方法:

我不知道你是想在某处硬编码一个神奇的字符串还是通过某种参数传入,但你可以这样做:

public static IEnumerable<string> GetFiles( 
    string path, string searchPatternExpression = "", 
    SearchOption searchOption = SearchOption.AllDirectories, 
    params string[] toIgnore) 
{ 
    Regex reSearchPattern = new Regex(searchPatternExpression); 
    return Directory.EnumerateFiles(path, "*", searchOption) 
                    .Where(file => reSearchPattern.IsMatch(System.IO.Path.GetExtension(file))) 
                    .Where(file => !toIgnore.Contains(file)); 
} 

(当然这很简单,如果你关心套管,但应该是一个开始。)

编辑

如果您想要不区分大小写的搜索,您可以将其更改为:

public static IEnumerable<string> GetFiles( 
    string path, string searchPatternExpression = "", 
    SearchOption searchOption = SearchOption.AllDirectories, 
    params string[] toIgnore) 
{ 
    var hash = new HashSet<string>(toIgnore, StringComparer.InvariantCultureIgnoreCase); 
    Regex reSearchPattern = new Regex(searchPatternExpression); 
    return Directory.EnumerateFiles(path, "*", searchOption) 
                    .Where(file => reSearchPattern.IsMatch(System.IO.Path.GetExtension(file))) 
                    .Where(file => !hash.Contains(file)); 
} 

编辑2

如果您想跳过具有给定名称的目录,请尝试:

public static IEnumerable<string> GetFiles( 
    string path, string searchPatternExpression = "", 
    SearchOption searchOption = SearchOption.AllDirectories, 
    params string[] toIgnore) 
{ 
    var hash = new HashSet<string>(toIgnore, StringComparer.InvariantCultureIgnoreCase); 
    Regex reSearchPattern = new Regex(searchPatternExpression); 
    return Directory.EnumerateDirectories(path, "*", searchOption) 
                    .Where(folder => !hash.Contains(Path.GetDirectoryName(folder))) 
                    .SelectMany(x => Directory.EnumerateFiles(x, "*", searchOption)); 
} 

请注意,这将忽略所有 与您的忽略集匹配的子目录。