UserEsp
UserEsp

Reputation: 426

How to get a list of files with specific extension in list and specific parent directory

I have two enum for save the list of valid extensions in my search and other with the list of valid directories where I want to search the files. Here is my code:

 var ext = folder.IncludedExt;//type enum
 var subdirectories = folder.IncludeSubfolder;//type enum
 var myFiles = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories).
          Where(file => ext.Any(x => file.Name.EndsWith(x.ToString(), StringComparison.OrdinalIgnoreCase)) );

I'm trying with those lines, I'm getting a list of files with the right extensions but I don't know how to get just the files with the directory property that be in my sub directories list.

Help please and thanks in advance;

Upvotes: 0

Views: 263

Answers (2)

UserEsp
UserEsp

Reputation: 426

Finally I found it

  list1 = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories).ToList().
                       Where(file => ext.Any(x => file.Name.EndsWith(x.ToString(),
                       StringComparison.OrdinalIgnoreCase)) &&
                       subdirectories.Any(x => file.Directory.Name.Equals(x.ToString(), StringComparison.OrdinalIgnoreCase))).ToList();

Upvotes: 0

Amir Sasson
Amir Sasson

Reputation: 11493

Maybe you can think of returning a list of FileInfo by using the LINQ Select statement. Then, append the "Where" filter , you can query the extension of the file (instead of endsWith condition). Finnaly, You will be ending with a a list of fileInfo that holds much more data than the file path only. Does this help?

Upvotes: 1

Related Questions