Some time you might need to get the list of files in each sub-folder of a folder:

Then you can get get as this code:

protected void btnShowFile_Click(object sender, EventArgs e)
       {
           string path = txtFilePath.Text;
           DirectoryInfo dir = new DirectoryInfo(path);
           List<string> listOfFiles = new List<string>();
           foreach (DirectoryInfo directory in dir.GetDirectories())
           {
               foreach (FileInfo file in directory.GetFiles())
               {
                   listOfFiles.Add(file.Name);        
               }
           }

           gvShowFile.DataSource = listOfFiles;
           gvShowFile.DataBind();
           gvShowFile.Visible = true;
       }   

 

image

The Directory Structure:

image

Files in Directory:

image

image

image

Advertisements