可以指定使用通配符的目录路径? [英] Possible to specify directory path with a wildcard?

查看:798
本文介绍了可以指定使用通配符的目录路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面这段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
}

这篇关于可以指定使用通配符的目录路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆