Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:estdi*

would match all files in the directories:

c:estdictionary
c:estdirectory
c:estdig

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
483 views
Welcome To Ask or Share your Answers For Others

1 Answer

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...