How to get files in the subfolders too from directory

Viewed 10927

Hi I have to get files from a specified path in the directory. This is the method I wrote but I didn't get the files from the subfolders.

Private void getfiles(){
Directoryinfo info = new Directoryinfo(configurationmanager.appsettings["Targetroot"].tostring ());
if (info.exists){
     Gvfiles.datasource = info.GetFiles();
     Gvfiles.databind();
   }
}
4 Answers
private List<String> DirSearch(string sDir)
{
List<String> files = new List<String>();
try
{
    foreach (string f in Directory.GetFiles(sDir))
    {
        files.Add(f);
    }
    foreach (string d in Directory.GetDirectories(sDir))
    {
        files.AddRange(DirSearch(d));
    }
}
catch (System.Exception excpt)
{
    MessageBox.Show(excpt.Message);
}

    return files;
}
Related