Tutorial :Why does DirectoryInfo.GetFiles() match files that don't match the mask?


Basically, I have this code:

DirectoryInfo dir = new DirectoryInfo(@"\\MYNETWORK11\ABCDEFG\ABCDEFGHIJKL\00806\");  FileInfo[] files = dir.GetFiles("200810*");  

I expect it to match any files starting with 200810. However, it's matching files named

20070618_00806.bak and 20070817_00806.bak (the stars aren't in the filename, that was the only way I could include the underscore)

I tried it with dir from a command prompt, and it matches those files also. Why?


Maybe using C: as the example was not a good thing. The directory I'm actually querying is a network share \\MYNETWORK11\ABCDEFG\ABCDEFGHIJKL\00806\

If checking against the short name has anything to do with it, won't 20070817_00806.bak be 200708~1.bak? That doesn't match either


msdn states that

"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" will return "longfilename.txt" because the equivalent 8.3 file name format would be "longf~1.txt"."

Could this be the cause?


Try this from the command line:

dir /x 200810*  

The "/x" will make it show the short filenames, as well as the long filenames. This would let you see whether the short filename actually does start with "200810".


I can't reproduce this, either from the command line or in a test app:

c:\Users\Jon\Test>echo > 20070618_00806.bak    c:\Users\Jon\Test>echo > 2007081700806.bak    c:\Users\Jon\Test>dir 200810*   Volume in drive C is OS   Volume Serial Number is B860-7E20     Directory of c:\Users\Jon\Test    File Not Found  

And the C# app:

using System;  using System.IO;    class Test  {      static void Main()      {          foreach (var file in new DirectoryInfo(".").GetFiles("200810*"))          {              Console.WriteLine(file);          }      }  }  

(This doesn't print any results.)

Perhaps there's some OS setting somewhere which is making a difference... which OS are you using? (I'm on 32-bit Vista.)


GetFiles will search the long file name and the short filename...it's not somehow matching short file names is it?

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »