c# - Directory.GetFiles 方法上的 searchPattern 逻辑

标签 c# .net search

我想知道 Directory.GetFiles 方法的搜索模式逻辑是什么。我在搜索模式中使用星号通配符。如果我将“*”放在字符前面,我不明白在搜索时应用什么样的逻辑。

如果我把“*”放在字符前面,我会得到意想不到的结果,但如果我把“*”放在字符后面,结果是正确的。

这是文件夹中的文件列表、示例代码和结果。

enter image description here

字符前面有星号

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

意想不到的结果。应该是 1.为什么出来了“b_Request”但为什么没有包含“b”?

enter image description here

字符后面的星号

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

这是预期结果

enter image description here

是bug还是我想太多了?

最佳答案

这有点棘手,但不是错误。

星号 (*) 代表该位置中的零个或多个字符,问号 (?) 代表该位置中的零个或一个字符。

根据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. 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”的搜索路径将匹配任何以字母 1 结尾的路径,将返回路径 1 和路径 b_Request(使用 8.3 文件名格式 b_Requ~1)。

您可以引用here有关 8.3 文件名的更多信息。

关于c# - Directory.GetFiles 方法上的 searchPattern 逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39062076/

相关文章:

c# - 如何以编程方式启用/禁用 IE 代理设置?

c# - System.FormatException'发生在 MongoDB.Bson.dll - XXX 不是有效的 24 位十六进制字符串

c# - 从动态创建的 TextBox ASP.NET 中读取值(表内)

c# - Bitmap.Save 参数无效

c# - 我如何判断一个 Action 是否是 lambda 表达式?

c# - 从 getType().GetProperties() 中排除属性

c# - 使用 HttpWebRequest 收到的截断响应

algorithm - 如何创建左规范二叉搜索树?

php - Laravel 搜索查询 - 'OR' 和 'AND' 的 SQL 排序破坏了 foreach 循环查询

algorithm - 您如何为快速搜索编制索引文件?