C# DirectoryInfo.GetFiles wildcard search

ぐ巨炮叔叔 提交于 2019-12-20 03:36:26

问题


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.


回答1:


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 mentioned on 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.



来源:https://stackoverflow.com/questions/32896894/c-sharp-directoryinfo-getfiles-wildcard-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!