Thursday, September 15, 2011

Loop through all folders and display files in a Gridview using ASP.NET

Loop through all folders and display files in a Gridview using ASP.NET This is one of the usefull code snippet everybody looking for.
When I search google I found lot of methods to accomplish this task but all seems to be very complex and lengthy code.
This is just a 4 line code to loop through all the folders, including subfolder to get the file detals and display it in Gridview.
You may alter this based on your need.
    
 protected void Page_Load(object sender, EventArgs e)
     {
       ArrayList arrFileList = new ArrayList();
       string[] strFiles = Directory.GetFiles("C:\\Test", "*", SearchOption.AllDirectories);
       foreach (string fileName in strFiles)
       {  
         arrFileList.Add(fileName); 
       }  
       GridView1.DataSource = arrFileList;  
       GridView1.DataBind();  
    }