Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method to get all files within folder and subfolders that will return a list

I have a method that will iterate through a folder and all of its subfolders and get a list of the file paths. However, I could only figure out how to create it and add the files to a public List, but not how to return the list. Here's the method:

public List<String> files = new List<String>();  private void DirSearch(string sDir)     {         try         {             foreach (string f in Directory.GetFiles(sDir))             {                 files.Add(f);             }             foreach (string d in Directory.GetDirectories(sDir))             {                 DirSearch(d);             }         }         catch (System.Exception excpt)         {             MessageBox.Show(excpt.Message);         }     } 

So, i just call DirSearch() at some point in my code and it 'fills' the list with the paths, but I want to be able to use it multiple times to create different lists with different directories, etc.

like image 688
Wilson Avatar asked Jan 13 '13 16:01

Wilson


People also ask

How can I get a list of all the subfolders and files present in a directory using PHP?

PHP using scandir() to find folders in a directory The scandir function is an inbuilt function that returns an array of files and directories of a specific directory. It lists the files and directories present inside the path specified by the user.

Is there a way to get a list of files in a folder?

Press and hold the SHIFT key and then right-click the folder that contains the files you need listed. Click Open command window here on the new menu. A new window with white text on a black background should appear. o To the left of the blinking cursor you will see the folder path you selected in the previous step.


Video Answer


1 Answers

You can use Directory.GetFiles to replace your method.

 Directory.GetFiles(dirPath, "*", SearchOption.AllDirectories) 
like image 195
Tilak Avatar answered Oct 12 '22 22:10

Tilak