Finding all the files in a folder recursively after letting the user select the folder through the FileBrowserDialog

Hi,

A few days back I was creating a small utility program for myself. The program needed the user to select a file and then I would get all the file name recursively.

With Dot net framework this was pretty easy. To let the user select a particular file, We can use the inbuilt FolderBrowserDialog. The usage of the dialog is pretty easy.

We first have to create the object of the class. We can optionally give the first selected folder path with the help of the SelectedPath property. To show the dialog we use the following code.

DialogResult result = fbd1.ShowDialog();

      if (result == DialogResult.OK)

      {

    strFolderpath = fbd1.SelectedPath;

      }

Here if the user has selected a folder than the selected path will be stored in a string variable.

Now to get the list of all the files inside the folder selected we will have to use the System.IO.Directory class. I am also using a Generic list of string to store the full path of the file. We have to use a recursive function. If a folder exists in side the given folder then we call the same function again with the new folder path (The folder inside the given folder)

         private void AddFileList(string FolderPath)

        {

            foreach (string strFoldername in Directory.GetDirectories(FolderPath))

            {

                AddFileList(strFoldername);

            }

            foreach (string strFilename in Directory.GetFiles(FolderPath))

            {

                FileList.Add(strFilename);

            }

        }

That’s all we have to do. No to fill the file list in the generic list call the above function with the folder path that was selected by the user through the FolderBrowserDialog.

Thanks
Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia