检查数组中目录的名称

Check for name of a directory from an array

我正在开发一个程序,该程序应该扫描特定目录以查找其中具有特定名称的任何目录,如果找到它们,请告诉用户。

目前,我加载它搜索的名字的方式是这样的:

static string path = Path.Combine(Directory.GetCurrentDirectory(), @"database.txt");
static string[] database = File.ReadAllLines(datapath);

我将其用作查找特定目录时要查找的名称数组。我正在使用 foreach 方法这样做。

System.IO.DirectoryInfo di = new DirectoryInfo("C:\ExampleDirectory");
        foreach (DirectoryInfo dir in di.GetDirectories())
        {

        }

有没有办法查看文件“database.txt”中的任何名称是否与“C:\ExampleDirectory”中的任何目录名称匹配?

我能想到的唯一方法是:

System.IO.DirectoryInfo di = new DirectoryInfo(versionspath);
        foreach (DirectoryInfo dir in di.GetDirectories())
        {
            if(dir.Name == //Something...) {
            Console.WriteLine("Match found!");
            break;}
        }

但这显然行不通,我想不出任何其他方法来做到这一点。任何帮助将不胜感激!

您可以使用 LINQ 来完成此操作

var allDirectoryNames = di.GetDirectories().Select(d => d.Name);
var matches = allDirectoryNames.Intersect(database);

if (matches.Any())
    Console.WriteLine("Matches found!");

在第一行我们得到了所有的目录名,然后我们使用Intersect()方法来查看在allDirectoryNamesdatabase

中都有哪些

像往常一样,LINQ 是必经之路。每当您必须在两个列表和包含不同类型的两个列表之间查找匹配或不匹配时,您将不得不使用 .Join().GroupJoin().

.Join() 发挥作用,如果你需要找到一个 1:1 关系和 .GroupJoin() 任何类型的一对关系(1:0,1:many 或 1:1).

因此,如果您需要与您的列表相匹配的目录,这对 .Join() 操作员来说是一项工作:

public static void Main(string[] args)
{
    // Where ever this comes normally from.
    string[] database = new[] { "fOo", "bAr" };
    string startDirectory = @"D:\baseFolder";

    // A method that returns an IEnumerable<string>
    // Using maybe a recursive approach to get all directories and/or files
    var candidates = LoadCandidates(startDirectory);

    var matches = database.Join(
        candidates,
        // Simply pick the database entry as is.
        dbEntry => dbEntry,
        // Only take the last portion of the given path.
        fullPath => Path.GetFileName(fullPath),
        // Return only the full path from the given matching pair.
        (dbEntry, fullPath) => fullPath,
        // Ignore case on comparison.
        StringComparer.OrdinalIgnoreCase);

    foreach (var match in matches)
    {
        // Shows "D:\baseFolder\foo"
        Console.WriteLine(match);
    }

    Console.ReadKey();
}

private static IEnumerable<string> LoadCandidates(string baseFolder)
{
    return new[] { @"D:\baseFolder\foo", @"D:\basefolder\baz" };
    //return Directory.EnumerateDirectories(baseFolder, "*", SearchOption.AllDirectories);
}

根据你在 Whosebug 上的其他问题,我假设你的问题是作业或者你是一个充满激情的爱好程序员,我是对的吗?所以我会在这里继续你几乎完整的解决方案来解释原理。
您将需要一个嵌套循环,一个循环中的循环。在外循环中,您遍历目录。你已经得到了这个。对于每个目录,您需要遍历 database 中的名称,以查看其中是否有任何项目与目录名称相匹配:

System.IO.DirectoryInfo di = new DirectoryInfo(versionspath);
foreach (DirectoryInfo dir in di.GetDirectories())
{
    foreach (string name in database)
    {        
        if (dir.Name == name)
        {
            Console.WriteLine("Match found!");
            break;
        }
    }
}

根据您的目标,您可能希望在第一个匹配的目录处退出。上面的示例代码没有。单个 break; 语句仅退出内部循环,而不退出外部循环。所以它继续检查下一个目录。尝试自己弄清楚如何在第一次匹配时停止(通过退出外循环)。