c# GetDirectories returning unexpected directories (specific example given)

259 Views Asked by At

Using c# GetDirectories to retrieving folders immediately under the search directory. The code works for all other directories searched EXCEPT a single directory. Specifically, GetDirectories returns two different folders, one contains the search pattern while the other one doesn't contain the search pattern.

Wildcard search pattern is *43* in my example.

Tried pretty much everything I could think of.
Strangely, if I change the case of any one character of the "WINCE_TEST_IMAGE_41" folder name then the GetDirectories returns the expected (and correct) single directory that matches the search pattern which is "*43*" (again). For example, change the "C" to a "c"...then the single directory containing 43 is returned...

Setup:
I have .NET Framework SDK 4.8 installed (latest installation as of 9/3/2019).
1. Create folder "C:\Temp\WINCE_OS_IMAGES"
2. Create subfolders in folders named:
* "WINCE_TEST_IMAGE_40"
* "WINCE_TEST_IMAGE_41"
* "WINCE_TEST_IMAGE_42"
* "WINCE_TEST_IMAGE_43"
* "WINCE_TEST_IMAGE_44".

So that the directory structure is:

C:\Temp
      \WINCE_OS_IMAGES
            \WINCE_TEST_IMAGE40
            \WINCE_TEST_IMAGE41
            \WINCE_TEST_IMAGE42
            \WINCE_TEST_IMAGE43
            \WINCE_TEST_IMAGE44

Code:
v is a parameter input to the method so I have assigned a value to help reproduce the issue.

string v = "43";

string[] dirs2 = Directory.GetDirectories(@"C:\Temp\WINCE_OS_IMAGES", "*" + 
    v + "*", SearchOption.TopDirectoryOnly);

foreach (string str in dirs2)
{
    Console.WriteLine(str);
}

Expected: GetDirectories statement returns only the directory ending in "43".

Actual:
GetDirectories statement returns two directories, one ending in "41" and the other ending in "43".

2

There are 2 best solutions below

0
Olivier Martial Soro On
string[] dirs2 = Directory.GetDirectories(@"C:\Temp\WINCE_OS_IMAGES", "*" +
            v + "*", SearchOption.TopDirectoryOnly);

Remove the white space.

0
Olivier Jacot-Descombes On

The documentation for GetFiles (didn't find this statement for GetDirectories) says:

Note
Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. For example, using a search pattern of "*1*.txt" returns "longfilename.txt" because the equivalent 8.3 file name format is "LONGFI~1.TXT".

I assume that the same is true for GetDirectories.

Therefore, you will have to filter the result yourself (e.g. with regex or with string.Contains()).

var dirs2 = Directory.GetDirectories(@"C:\Temp\WINCE_OS_IMAGES", "*" + 
                                     v + "*", SearchOption.TopDirectoryOnly)
    .Where(d => d.Contains(v));

foreach (string str in dirs2)
{
    Console.WriteLine(str);
}

Note: you are searching for a number, therefore the character case is not an issue. If you need a case insensitive search, you cannot simply use Contains, since it has no overload allowing case insensitive search. You could use Colonel Panic's answer to the question Case insensitive 'Contains(string)' or compare with

v = v.ToLowerInvariant();
...
.Where(d => d.ToLowerInvariant().Contains(v));