C#DirectoryInfo.GetFiles通配符搜索通配符、DirectoryInfo、GetFiles

由网友(3.仙女有只猫)分享简介:我遇到以下code段的行为差异DirectoryInfo的迪=新的DirectoryInfo(C:\);的FileInfo [] TEXTFILES = di.GetFiles(登陆_ ??? ??? TXT。);在哪里?是通配符0或1个字符,所以这应该返回匹配的图案在路径文件:日志_ .. TXTlog_0.0....

我遇到以下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目标,我没有得到任何比赛。

SQL必知必会 第六课 用通配符进行过滤 使用LIKE操作符, 通配符进行通配搜索

如果我改变从

的通配符模式

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

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

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

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

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

解决方案

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

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

?是通配符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:

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

阅读全文

相关推荐

最新文章