如何收集在一个文件夹中的所有文件和子文件夹匹配字符串 [英] How to collect all files in a Folder and its Subfolders that match a string
本文介绍了如何收集在一个文件夹中的所有文件和子文件夹匹配字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C#中我如何通过一个文件夹及其子文件夹搜索找到匹配的字符串值的文件。我的字符串值可能是ABC123和配套文件可能ABC123_200522.tif。可阵列收集这些?先谢谢了。
-Josh
解决方案
无效DirSearch(字符串SDIR)
{
尝试
{
的foreach(字符串d。在Directory.GetDirectories(SDIR))
{
的foreach(在Directory.GetFiles字符串型F(D,SMATCH))
{
lstFilesFound.Add(F);
}
DirSearch(四);
}
}
赶上(System.Exception的excpt)
{
Console.WriteLine(excpt.Message);
}
其中, SMATCH
是要搜索什么样的标准。
In C# how can I search through a Folder and its Subfolders to find files that match a string value. My string value could be "ABC123" and a matching file might be ABC123_200522.tif. Can an Array collect these? Thanks in advance.
-Josh
解决方案
void DirSearch(string sDir)
{
try
{
foreach (string d in Directory.GetDirectories(sDir))
{
foreach (string f in Directory.GetFiles(d, sMatch))
{
lstFilesFound.Add(f);
}
DirSearch(d);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
where sMatch
is the criteria of what to search for.
这篇关于如何收集在一个文件夹中的所有文件和子文件夹匹配字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文