C#DirectoryInfo.GetFiles通配符搜索 [英] C# DirectoryInfo.GetFiles wildcard search

查看:520
本文介绍了C#DirectoryInfo.GetFiles通配符搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下code段的行为差异

  DirectoryInfo的迪=新的DirectoryInfo(C:\);
的FileInfo [] TEXTFILES = di.GetFiles(登陆_ ??? ??? TXT。);
 

在哪里?是通配符0或1个字符,所以这应该返回匹配的图案在路径文件:

 日志_ .. TXT
log_0.0.txt
log_00.00.txt
log_000.000.txt
 

当编译为Windows .NET框架3.5(桌面)所有这些文件都返回,但在嵌入式框架3.5嵌入式的Windows CE 6的.NET Compact目标,我没有得到任何比赛。

如果我改变从

的通配符模式

 的FileInfo [] TEXTFILES = di.GetFiles(登陆_ ??? ??? TXT。);
 

 的FileInfo [] TEXTFILES = di.GetFiles(登陆_ * TXT。);
 

然后,我得到的所有的文件有望在上面的图案。

有谁知道为什么是这样? 文件的目标平台上肯定存在。

有关这个问题的范围之外的原因,我强烈要求至少要理解为什么这是行不通的。

解决方案

我看到一对夫妇的问题。我不知道,如果你离开的东西出来,故意保持这个问题简单,如果你错过了这些东西,所以我列出了所有我所看到的问题:

  1. 您使用的不是逐字字符串。 DirectoryInfo的迪=新的DirectoryInfo(C:\); 不编译,因为PTED作为转义字符\是除$ P $。同样的例子与逐字字符串是 DirectoryInfo的迪=新的DirectoryInfo(@C:\); 这确实编译
  2. 的Windows CE / Mobile不具备的驱动器号的概念。 DirectoryInfo的迪=新的DirectoryInfo(@C:\); 在桌面上等同于 DirectoryInfo的迪=新的DirectoryInfo(@\); 的CE /移动。
  3. 本报价由你是不正确的:
      

    ?是通配符0或1字符

它实际上提到通配符为1个字符在MSDN

  

中的星号(*)和问号(?)作为通配符   字符,因为它们是在MS-DOS和视窗。星号匹配   任何字符序列,而问号匹配任何   单个字符

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

这篇关于C#DirectoryInfo.GetFiles通配符搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆