可以指定使用通配符的目录路径?通配符、路径、目录

2023-09-03 01:26:00 作者:指忘

我有下面这段code:

I have the following piece of code:

foreach (string file in Directory.GetFiles(sourcePath))
{
    // whatever
}

这从一个特定的目录获取文件。是否可以使用通配符匹配目录?例如:

That gets files from a specific directory. Would it be possible to match directories using a wildcard? For example:

c:\test\di*

将匹配在目录中的所有文件:

would match all files in the directories:

c:\test\dictionary\
c:\test\directory\
c:\test\dig\

我看到你可以通过文件过滤器GetFiles方法,但仅适用于文件,而不是目录名。

I saw that you can pass a file filter to the GetFiles method, but that applies to files only, not directory names.

推荐答案

您有超载这允许你指定一个搜索模式,或者如果你需要指定的搜索选项还有另一个重载:

You have an overload for this which allows you to specify a search pattern or if you need to specify search options there's another overload:

foreach (string directory in Directory.GetDirectories(sourcePath, "di*"))
{
    // whatever
}