1

我有这段可爱的代码,它为我做了出色的工作,只需抓取我需要进行多次搜索等的所有文件。

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() 并返回列表中回调的每个字符串。

* Button > GetFilesPassThrough> Creates list with Get Files> 并写入文本文件 *

4

2 回答 2

2
 if (!(file.Contains("Narnia"))) writer.WriteLine(file)

Or am I being too simplistic and misunderstanding?

于 2013-10-15T16:13:41.263 回答
2

I don't know if you're looking to hard code a magic string somewhere or pass in via some sort of params, but you could do something like:

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));
}

(Of course this is simplistic, if you care about casing, but should be a start.)

Edit

If you want a case-insensitive search, you could change it to look like:

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));
}

Edit 2

If you're looking to skip the directory with a given name, try:

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));
}

Note that this is going to ignore all subdirectories that match your ignore set.

于 2013-10-15T16:22:59.393 回答