How to get Files from a Directory Using LINQ

Hi... Use below Query to get your desired files type collection:
  1. string FolderPath = @"D:\AllFiles";  
  2. DirectoryInfo di = new DirectoryInfo(FolderPath)  
  3.   
  4. //Get All csv Files  
  5. List getAllCSVFiles = di.GetFiles("*.csv")  
  6.                                 .Where(file => file.Name.EndsWith(".csv"))  
  7.                                 .Select(file => file.Name).ToList();  
  8.   
  9. //Get All Notepad Files  
  10. List getAllNotePadFiles = di.GetFiles("*.txt")  
  11.                                 .Where(file => file.Name.EndsWith(".txt"))  
  12.                                 .Select(file => file.Name).ToList(); 
 Just pass your file extension and enjoy
 
Ebook Download
View all
Learn
View all