如何遍历在不包含特定字符的文件夹处停止的目录?

How do I iterate through a directory stopping at a folder that excludes a specific character?

我想遍历一个目录并在第一个以“@”结尾的文件夹处停止

这是我目前尝试的方法(基于本网站的另一个问题):

string rootPath = "D:\Pending\Engineering\Parts\3";
string targetPattern = "*@";

string fullPath = Directory
                 .EnumerateFiles(rootPath, targetPattern, SearchOption.AllDirectories)                                               
                 .FirstOrDefault();

if (fullPath != null)
    Console.WriteLine("Found " + fullPath);
else
    Console.WriteLine("Not found");

我知道 *@ 不正确,不知道该怎么做。
我也有问题 SearchOption Visual studio 说 "it's an ambiguous reference."

最终我希望代码获取此文件夹的名称并使用它来重命名其他文件夹。

最终解决方案

我最终结合使用了 dasblikenlight 和 user3601887

string fullPath = Directory
                   .GetDirectories(rootPath, "*", System.IO.SearchOption.TopDirectoryOnly)
                   .FirstOrDefault(fn => !fn.EndsWith("@"));

由于EnumerateFiles模式不支持正则表达式,需要获取所有目录,在C#端做过滤:

string fullPath = Directory
    .EnumerateFiles(rootPath, "*", SearchOption.AllDirectories)                                               
    .FirstOrDefault(fn => !fn.EndsWith("@"));

或者将 EnumerateFiles 替换为 GetDirectories

string fullPath = Directory
                 .GetDirectories(rootPath, "*@", SearchOption.AllDirectories)
                 .FirstOrDefault();