ChriPf
ChriPf

Reputation: 2780

Directory.GetFiles(...) with pattern *.??? returns funny results

I am using following Code:

var di = new DirectoryInfo("path/to/dir");
var matchingFiles = di.GetFiles("*.???);

As expected, files like

are returned. However, there are some funny results. As the MS-documentation mentions, the shortname is searched too. I also found the information about the dot-selector working in .NET 4+. Still I cannot see this explains following results:

Q1: How can this results be explained.

Q2: How can a match for pattern *.[3 arbitrary chars] be achieved.

Upvotes: 1

Views: 116

Answers (2)

Richard
Richard

Reputation: 109180

How can this results be explained.

Short names, as you note, are always searched; and they always have a three character extension (even if those characters are spaces).

How can a match for pattern *.[3 arbitrary chars] be achieved.

Do the check in your code (eg. use a regex). MS-DOS wildcards are very limited, and backwards compatibility requirements makes them yet weaker; they are really only useful for specific matches and not general filtering.

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460340

Q2) You could use LINQ and the Path class instead:

var files = Directory.EnumerateFiles("path/to/dir", "*.*")
    .Where(file => Path.GetExtension(file).TrimStart('.').Length == 3);

Upvotes: 3

Related Questions