简体   繁体   English

Directory.GetFiles方法上的searchPattern逻辑

[英]searchPattern Logic on Directory.GetFiles Method

I would like to know what is the search pattern logic on Directory.GetFiles Method. 我想知道Directory.GetFiles方法上的搜索模式逻辑是什么。 I use asterisk wildcard on my search pattern. 我在搜索模式中使用星号通配符。 I do not understand what kind of logic to apply on searching if i put "*" in-front of char. 如果我在char前面加“ *”,我不明白在搜索时应采用哪种逻辑。

I got unexpected result if i put "*" in-front of char but it was correct if i put at behind of char. 如果在char的前面加上“ *”,则会得到意外的结果,但是如果在char的后面放置,这是正确的。

Here is file list in folder, sample code and result. 这是文件夹中的文件列表,示例代码和结果。

在此处输入图片说明

Asterisk in-front of char 字符前面的星号

string _strSearchPattern = "*1";
foreach (string _strFolder in Directory.GetFiles(@"C:\Temp\FileList", _strSearchPattern))
Console.WriteLine("{0}", _strFolder);

Unexpected result. 意外的结果。 It should be 1. Why "b_Request" is come out but why not "b" is include? 应该为1。为什么出现“ b_Request”,但是为什么不包括“ b”?

在此处输入图片说明

Asterisk behind of char 字符后面的星号

string _strSearchPattern = "1*";
foreach (string _strFolder in Directory.GetFiles(@"C:\Temp\FileList", _strSearchPattern))
Console.WriteLine("{0}", _strFolder);

Here is expected result 这是预期的结果

在此处输入图片说明

Is it bug or i'm thinking too much? 是bug还是我想得太多?

That is sort of tricky but not a bug. 这有点棘手,但不是错误。

Asterisk (*) stands for zero or more characters in that position and question mark (?) stands for zero or one character in that position. 星号(*)代表该位置的零个或多个字符,问号(?)代表该位置的零个或一个字符。

According to MSDN : 根据MSDN

Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. 因为此方法将同时检查8.3文件名格式和长文件名格式的文件名,所以类似于“ * 1 * .txt”的搜索模式可能会返回意外的文件名。 For example, using a search pattern of "*1*.txt" returns "longfilename.txt" because the equivalent 8.3 file name format is "LONGFI~1.TXT". 例如,使用搜索模式“ * 1 * .txt”将返回“ longfilename.txt”,因为等效的8.3文件名格式为“ LONGFI〜1.TXT”。

In you first case, a search path with "*1" will match any path ends with letter 1, path 1 and path b_Request (with the 8.3 file name format b_Requ~1) will be returned. 在第一种情况下,带有“ * 1”的搜索路径将匹配以字母1结尾的任何路径,路径1和路径b_Request(文件名格式为8.3 b_Requ〜1)。

You can refer here for more about 8.3 filename. 您可以在此处参考有关8.3文件名的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM