Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory.GetDirectories returns wrong results with a pattern of *.* [duplicate]

Assume you have a directory that contains the following directories:

dontgetme
get.me

If you use the following code:

string[] directories = Directory.GetDirectories(rootDirectory, "*.*", SearchOption.TopDirectoryOnly);

You would expect directories to contain:

get.me

But it contains both directories, why is this?

Looking into the documentation the only wildcards are * and ? so they shouldn't be affecting this.

Further if you use the pattern *. only the dontgetme directory is returned; but using *.*.*.* etc. both are still returned.


In case it matters I am using .NET 4.6.1 and C# 6 on Windows 10.

like image 790
TheLethalCoder Avatar asked Jan 23 '26 22:01

TheLethalCoder


2 Answers

You pattern means "anything with any extension". No extension is "any extension", so you match all four.

You could try ?*.?* as pattern so you get those that have at least one character as both extension and name.

like image 82
nvoigt Avatar answered Jan 26 '26 14:01

nvoigt


Windows file and directory names always have an extension, even if it's blank. So *.* matches anything because it's treated as anything.

like image 32
DavidG Avatar answered Jan 26 '26 14:01

DavidG