Pages

Monday, May 18, 2009

List all files in a folder in an asp.net gridview

We do have a folder (and a number of sub folders) with a number of various files in those folders (and sub folders). Now we do need to list all those files in an asp.net gridview control [for the purpose of organizing those files, or for the preview of files uploaded till now, say]. We can do it in asp.net using the System.IO namespace. In this article, I am going to show the coding for displaying all files in a folder in an asp.net gridview. In fact, as a bonus, we will do the listing of all files in a root folder and all its subfolders also.

Fig: Snapshot of displaying all files in a folder and its subfolders in an asp.net gridview control

Design Page Markup (ListFiles.aspx)
We first need to have a web page with the gridview control in it.

<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
</asp:GridView>
</div>
</form>

Remember, we have set the AutoGenerateColumns field to true. So all the fields that is present in our source will be displayed by the gridview, each as a separate column. (I have removed the formatting of the gridview markup to make the code clear. Please your own formatting for the gridview.)

Code behind page (ListFiles.aspx.cs)
Now here goes the actual code to bind all the files in the folder "Resources", that resides at the root of the web site, and all its subfolders.

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetFilesAndFolders();
}
}

public void GetFilesAndFolders()
{
DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("~/Resources"));
FileInfo[] fileInfo = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);
GridView1.DataSource = fileInfo;
GridView1.DataBind();
}

We have used the DirectoryInfo and FileInfo classes of the System.IO namespaces. The DirectoryInfo class will get the root directory of the provided folder name. And we have used the DirectoryInfo class's GetFiles method. This method has two overloads. We can specify the file patterns (e.g. "*.txt" for text files only, or "*.*" for all types of files etc) and the search option- if to list all the files in all the subdirectories of the root directory (in our case, "Resources" folder) provided as the input, or just to list the files in the root directory.

When we browse the page, we see the gridview populated with 13 fields, which I hope need no explanation. However for your reference I have listed the fields here: LastAccessTimeUtc, Length, LastWriteTime, LastWriteTimeUtc, FullName, IsReadOnly, CreationTimeUtc, LastAccessTime, CreationTime, Name, Exists, Extention, and DirectoryName. For the description of each field, please visit FileInfo class Properties, msdn document.

Now from those thirteen fields, we can choose and display the required ones. Please don't forget to share your views, suggestions or comments as always. Thank you. Happy programming!

kick it on DotNetKicks.com pimp it Shout it

11 comments:

  1. That post is really nice! Saved me a lot of time.
    I modified it according to my purpose.
    Thanks A Lot
    http://freshwebdeveloper.blogspot.com

    ReplyDelete
  2. Good documentation, it really gives nice tips for retrive folders

    ReplyDelete
  3. very useful blog.

    ReplyDelete
  4. Thank you very much !! It was very very helpful. :-)

    ReplyDelete
  5. Thankyou... :)

    ReplyDelete
  6. How can a hyperlinked image be added to this gridview?

    The image (.jpg) is located in the same folder.

    ReplyDelete
  7. good article. Thanks

    ReplyDelete
  8. Great article, What if you want to click on the link? I'm gettin an error trying to open the file. Is there something additional that needs to be set in IIS to actually open &| download the file?

    ReplyDelete
  9. -->In .aspx








    -->In .aspx.cs

    using System.IO;


    protected void Page_Load(object sender, EventArgs e)
    {

    BuildTree();

    }

    private void BuildTree()
    {
    //get root directory
    DirectoryInfo rootDir = new DirectoryInfo(Server.MapPath("~/"));

    //create and add the root node to the tree view
    TreeNode rootNode = new TreeNode(rootDir.Name, rootDir.FullName);
    TreeView1.Nodes.Add(rootNode);

    //begin recursively traversing the directory structure
    TraverseTree(rootDir, rootNode);
    }

    private void TraverseTree(DirectoryInfo currentDir, TreeNode currentNode)
    {
    //loop through each sub-directory in the current one
    foreach (DirectoryInfo dir in currentDir.GetDirectories())
    {
    //create node and add to the tree view
    TreeNode node = new TreeNode(dir.Name, dir.FullName);
    currentNode.ChildNodes.Add(node);

    //recursively call same method to go down the next level of the tree
    TraverseTree(dir, node);
    }


    foreach (FileInfo File in currentDir.GetFiles())
    {
    //create node and add to the tree view
    TreeNode node = new TreeNode(File.Name, File.FullName);
    currentNode.ChildNodes.Add(node);
    }
    }

    ReplyDelete
  10. What needs to be done if there are many files and paging is added to the page?

    ReplyDelete

Hope you liked this post. You can leave your message or you can put your valuable suggestions on this post here. Thanks for the sharing and cooperation!