简体   繁体   English

在另一组文件中搜索具有特定扩展名的文件的最佳方法是什么

[英]What is the best way of searching a files with specific extension in another group of files

I want to write a search algoritham for searching files having extension .txt(the occurance of all .txt file) in another group of files.ie i have a directory named d:\\myfolder.I have a files of extension .txt in this folderin as well as subfolders. 我想写一个搜索算法来搜索另一组文件中扩展名为.txt(所有.txt文件的出现)的文件。即,我有一个名为d:\\ myfolder的目录。我有一个扩展名为.txt的文件folderin以及子文件夹。 There are also files of extension .proj,.dat,.cs etc in this folder as well as subfolders.I want to search the occurance of .txt files in these files I thought to create a list(LISTA) that contain the names of all .txt files and also create a seperate list(ListB) containg the fullpath of all other files.Then iterate throgh the lists to get the result.Is that a better method.Else is there any other better method 该文件夹中还有子文件夹.proj,.dat,.cs等扩展名。我想在这些文件中搜索.txt文件的出现情况,我想创建一个包含以下名称的列表(LISTA)所有.txt文件,并创建一个包含所有其他文件的完整路径的单独列表(ListB)。然后遍历列表以获取结果。是一个更好的方法。还有其他更好的方法

If you want to search for a particular file in folder and sub folder then easiest would be to use Directory.GetFiles like 如果要在文件夹和子文件夹中搜索特定文件,那么最简单的方法是使用Directory.GetFiles

string path = @"C:\YourFolder";
string[] txtFiles = System.IO.Directory.GetFiles(path, 
                                                 "*.txt", 
                                                SearchOption.AllDirectories);

You don't have to populate two different lists, one with txt extensions and one without. 您不必填充两个不同的列表,一个带有txt扩展名,一个没有txt扩展名。 Directory.GetFiles would let you find all the txt files in a folder and sub folder. Directory.GetFiles将使您可以找到一个文件夹和子文件夹中的所有txt文件。

if you use LINQ: 如果您使用LINQ:

 List<FileInfo> list = new List<FileInfo>();
            list= list.Where(a => a.Extension == Path.GetExtension(a.FullName)).ToList();

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

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