简体   繁体   中英

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. 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

If you want to search for a particular file in folder and sub folder then easiest would be to use Directory.GetFiles like

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. Directory.GetFiles would let you find all the txt files in a folder and sub folder.

if you use LINQ:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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