递归到一个list表里,求List的count值就好
递归搜索
List
private void Form1_Load(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo("文件夹名称");
bianli(dir, "关键字");
MessageBox.Show("总共有" + strs.Count + "个文件有关键字");
}
///
/// 遍历文件夹并找出包含特定关键字的文件
///
/// 文件夹
/// 关键字
void bianli(DirectoryInfo dir,string str)
{
FileInfo[] files = dir.GetFiles();
foreach (FileInfo f in files)
{
if (f.Name.IndexOf(str) >= 0)
{
strs.Add(f.Name);
}
}
DirectoryInfo[] dirs = dir.GetDirectories();
foreach (DirectoryInfo d in dirs)
{
bianli(d, str);
}
}