C# DirectoryInfo.GetFiles 通配符搜索

标签 c# .net compact-framework

我在以下代码段中遇到行为差异

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");

然后我得到了上述模式中的所有预期文件。

有谁知道为什么会这样吗? 这些文件肯定存在于目标平台上。

由于超出此问题范围的原因,我强烈希望至少了解为什么这不起作用。

最佳答案

我发现了几个问题。我不知道您是否为了让问题简单而故意遗漏了一些内容,或者您​​是否错过了这些内容,所以我列出了我看到的所有问题:

  1. 您没有使用逐字字符串文字。 DirectoryInfo di = new DirectoryInfo("c:\"); 无法编译,因为“\”被解释为转义字符。逐字字符串文字的相同示例是 DirectoryInfo di = new DirectoryInfo(@"c:\"); ,它确实可以编译。
  2. Windows CE/Mobile 没有驱动器盘符的概念。桌面上的 DirectoryInfo di = new DirectoryInfo(@"c:\"); 相当于 CE/Mobile 上的 DirectoryInfo di = new DirectoryInfo(@"\");
  3. 您的这段引用是不正确的:

    ? 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/

相关文章:

c# - 十进制 DbParameter 的精度问题

c# - 反序列化不同数据的问题

C#:以自定义百分比创建 CPU 使用率

C#与远程数据库连接(使用Compact)

.net - IsNumeric 在评估对象时抛出 FormatException

c# - 异常堆栈跟踪可以为空吗?

c# - 如何为 System.Windows.Fontstyle 设置粗体字样?

c# - IEnumerable 的多个枚举器

c# - 最大函数 Linq 查询 - Nullable Int 不是 IEnumerable?

.net - 我能否创建一个与标准 .NET Framework 和 .NET Compact Framework 兼容的 DLL?