简体   繁体   中英

C# DirectoryInfo.GetFiles wildcard search

I am experiencing differences in behavior in the following code segment

DirectoryInfo di = new DirectoryInfo("c:\");
FileInfo[] textFiles = di.GetFiles("log_???.???.txt");

Where ? is the wildcard for 0 or 1 characters, so this should return files in the path matching the patterns:

log_..txt
log_0.0.txt
log_00.00.txt
log_000.000.txt

All of these files are returned when compiled for Windows .NET framework 3.5 (desktop), but on the target embedded Windows CE 6 with .NET Compact Embedded Framework 3.5, I get no matches.

If I change the wildcard pattern from

FileInfo[] textFiles = di.GetFiles("log_???.???.txt");

to

FileInfo[] textFiles = di.GetFiles("log_*.*.txt");

Then I get all of the expected files in the pattern above.

Does anybody know why this is the case? The files definitely exist on the target platform.

For reasons outside the scope of this question, I do strongly desire at least to understand why this is not working.

I see a couple of issues. I don't know if you left stuff out on purpose to keep the question simple, or if you missed these things, so I am listing all the problems I see:

  1. You're not using verbatim string literals. DirectoryInfo di = new DirectoryInfo("c:\\"); doesn't compile because the '\\' is interpreted as an escape character. The same example with verbatim string literals is DirectoryInfo di = new DirectoryInfo(@"c:\\"); which does compile.
  2. Windows CE/Mobile doesn't have a concept of drive letters. DirectoryInfo di = new DirectoryInfo(@"c:\\"); on desktop is equivalent to DirectoryInfo di = new DirectoryInfo(@"\\"); on CE/Mobile.
  3. This quote from you is incorrect:

    ? is the wildcard for 0 or 1 characters

It's actually a wildcard for 1 character as mentionedon MSDN

The asterisk (*) and question mark (?) are used as wildcard characters, as they are in MS-DOS and Windows. The asterisk matches any sequence of characters, whereas the question mark matches any single character .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM