在文件夹中的所有文件名中出现字符串
本文关键字:文件名 字符串 文件夹 | 更新日期: 2023-09-27 18:26:35
我想知道一个文件夹中有多少文件名包含一个特定的字符串。我不知道该如何使用C#。
例如,在文件夹D:''A中,有
fileApple_1.bmp
fileApple_2.bmp
fileApple_3.bmp
fileApple_4.bmp
fileApple_5.bmp
fileOrange_1.bmp
fileOrange_2.bmp
fileOrange_3.bmp
fileOrange_4.bmp
fileOrange_5.bmp
fileGrape_1.bmp
fileGrape_2.bmp
fileGrape_3.bmp
我想知道有多少文件的名字包含"葡萄",我们知道答案应该是3,因为
fileGrape_1.bmp
fileGrape_2.bmp
fileGrape_3.bmp
是名称中包含"Grape"的文件。
谢谢!
string searchTerm = "grape";
int grapeCount =
new DirectoryInfo(directoryPath)
.EnumerateFiles(string.Format("*{0}*", searchTerm))
.Count();
string keyword = "Grape";
string[] files = Directory.GetFiles("D:''", "*" + keyword + "*");
然后,您可以检索文件数组的Length属性来查找的出现次数