我在以下代码段中遇到行为差异
DirectoryInfo di = new DirectoryInfo("c:\");
FileInfo[] textFiles = di.GetFiles("log_???.???.txt");
哪里?是 0 或 1 个字符的通配符,因此这应该返回路径中与模式匹配的文件:
log_..txt
log_0.0.txt
log_00.00.txt
log_000.000.txt
针对 Windows .NET Framework 3.5(桌面)编译时,所有这些文件都会返回,但在带有 .NET Compact Embedded Framework 3.5 的目标嵌入式 Windows CE 6 上,我没有得到任何匹配项。
如果我更改通配符模式
FileInfo[] textFiles = di.GetFiles("log_???.???.txt");
至
FileInfo[] textFiles = di.GetFiles("log_*.*.txt");
然后我得到了上述模式中的所有预期文件。
有谁知道为什么会这样吗? 这些文件肯定存在于目标平台上。
由于超出此问题范围的原因,我强烈希望至少了解为什么这不起作用。
最佳答案
我发现了几个问题。我不知道您是否为了让问题简单而故意遗漏了一些内容,或者您是否错过了这些内容,所以我列出了我看到的所有问题:
- 您没有使用逐字字符串文字。
DirectoryInfo di = new DirectoryInfo("c:\");
无法编译,因为“\”被解释为转义字符。逐字字符串文字的相同示例是DirectoryInfo di = new DirectoryInfo(@"c:\");
,它确实可以编译。 - Windows CE/Mobile 没有驱动器盘符的概念。桌面上的
DirectoryInfo di = new DirectoryInfo(@"c:\");
相当于 CE/Mobile 上的DirectoryInfo di = new DirectoryInfo(@"\");
。 - 您的这段引用是不正确的:
? is the wildcard for 0 or 1 characters
如上所述,它实际上是 1 个字符的通配符 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.
关于C# DirectoryInfo.GetFiles 通配符搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32896894/