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

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

c# - Possible to specify directory path with a wildcard?

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

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

1 Answer

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...