C#搜索某些文件夹以外的文件和文件夹

时间:2013-10-16 12:29:37

标签: c# .net linq file directory

有没有办法使用LINQ命令like this

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

This帮助了例外

4 个答案:

答案 0 :(得分:6)

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

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

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

答案 1 :(得分:3)

使用简单的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;
}

答案 2 :(得分:0)

试试这个

 var s2 =  Directory.GetDirectories(dirPath, "*", SearchOption.AllDirectories)
         .Where(directory => !directory.Contains("DirectoryName"));

答案 3 :(得分:0)

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