5

有没有办法使用这样的 LINQ 命令从 SearchOption 中排除某些目录

string path = "C:\SomeFolder";

var s1 = Directory.GetFiles(path , "*.*", SearchOption.AllDirectories);

var s2 = Directory.GetDirectories(path , "*.*", SearchOption.AllDirectories);

该路径由包含某些文件的 Sub1 和 Sub2 文件夹组成。我需要将它们从目录搜索中排除。

谢谢

这有效:

string[] exceptions = new string[] { "c:\\SomeFolder\\sub1",
"c:\\SomeFolder\\sub2" };

var s1 = Directory.GetFiles("c:\\x86", "*.*",
SearchOption.AllDirectories).Where(d => exceptions.All(e =>
!d.StartsWith(e)));

有助于异常

4

4 回答 4

9

不,据我所知没有。但是您可以使用非常简单的 LINQ 在一行中完成此操作。

var s1 = Directory.GetFiles(path , "*.*", SearchOption.AllDirectories).Where(d => !d.StartsWith("<EXCLUDE_DIR_PATH>")).ToArray();

您也可以轻松组合多个排除 DIR。

于 2013-10-16T12:33:03.453 回答
5

使用简单的 LINQ 方法无法完全满足您的需求。您将需要编写递归例程而不是使用SearchOption.AllDirectories. 原因是您要过滤目录而不是文件

您可以使用以下静态方法来实现您想要的:

public static IEnumerable<string> GetFiles(
    string rootDirectory,
    Func<string, bool> directoryFilter,
    string filePattern)
{
    foreach (string matchedFile in Directory.GetFiles(rootDirectory, filePattern, SearchOption.TopDirectoryOnly))
    {
        yield return matchedFile;
    }

    var matchedDirectories = Directory.GetDirectories(rootDirectory, "*.*", SearchOption.TopDirectoryOnly)
        .Where(directoryFilter);

    foreach (var dir in matchedDirectories)
    {
        foreach (var file in GetFiles(dir, directoryFilter, filePattern))
        {
            yield return file;
        }
    }
}

你会像这样使用它:

var files = GetFiles("C:\\SearchDirectory", d => !d.Contains("AvoidMe", StringComparison.OrdinalIgnoreCase), "*.*");

为什么增加了复杂性? 这种方法完全避免在您不感兴趣的目录中查找。SearchOption.AllDirectories顾名思义,将在所有目录中搜索。

如果您不熟悉迭代器方法(yield return语法),可以用不同的方式编写:问吧!

选择

这具有几乎相同的效果。但是,它仍然会在您要忽略的目录的子目录中找到文件。也许这对你没问题;代码更容易理解。

public static IEnumerable<string> GetFilesLinq(
    string root,
    Func<string, bool> directoryFilter,
    string filePattern)
{
    var directories = Directory.GetDirectories(root, "*.*", SearchOption.AllDirectories)
        .Where(directoryFilter);

    List<string> results = new List<string>();

    foreach (var d in directories)
    {
        results.AddRange(Directory.GetFiles(d, filePattern, SearchOption.TopDirectoryOnly));
    }

    return results;
}
于 2013-10-16T13:02:31.983 回答
0

尝试这个

 var s2 =  Directory.GetDirectories(dirPath, "*", SearchOption.AllDirectories)
         .Where(directory => !directory.Contains("DirectoryName"));
于 2013-10-16T12:37:06.020 回答
-1
///used To Load Files And Folder information Present In Dir In dir
private void button1_Click(object sender, EventArgs e)
    {
        FileInfo[] fileInfoArr;
        StringBuilder sbr=new StringBuilder();
        StringBuilder sbrfname = new StringBuilder();
        string strpathName = @"C:\Users\prasad\Desktop\Dll";
        DirectoryInfo dir = new DirectoryInfo(strpathName);
        fileInfoArr = dir.GetFiles("*.dll");

        //Load Files From RootFolder
        foreach (FileInfo f in fileInfoArr)
        {
            sbrfname.AppendLine(f.FullName);
        }
        DirectoryInfo[] dirInfos = dir.GetDirectories("*.*");
       //Load Files from folder folder 
        foreach (DirectoryInfo d in dirInfos)
        {
           fileInfoArr = d.GetFiles("*.dll");
           foreach (FileInfo f in fileInfoArr)
           {
               sbrfname.AppendLine(f.FullName);
           }
            sbr.AppendLine(d.ToString());

        }
        richTextBox1.Text = sbr.ToString();
        richTextBox2.Text = sbrfname.ToString();
    }
于 2014-09-08T11:55:48.337 回答