Let us learn how to get name of files stored in a Folder in Grid View

Raj.Trivedi
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 1584 red flag

In this article we will see how we can check what are the files stored on the folder.

Introduction



In real world application we might have requirement to check the files stored in the folder on the server.In this article we will see how we can get a list of files in a grid.



Objective


  1. Uploading files to folder
  2. Get the list of files uploaded to the folder



Using the code


  1. Create a New Empty Web Site in VS2010.
  2. Add a New Folder named as FilesUploaded in the website by right clicking on the Website Name -> Add New Folder.
  3. Drag and Drop a File Uploader, 1 Button and 1 Grid View.
  4. We will upload the using the file uploader.
  5. To get the files name we will use a bindgridview function to the get the name of the files and all the info.

Explanation of bindgridview function

  1. We will be using DirectoryInfo class from System.IO creating its object and getting all the information from the directory from the folder that we have in our website. 
  2. Now we will create an array of the fileinfo class of System.IO namespace and get all the files from the folder using getfiles method of the object created of DirectoryInfo.

// HTML Mark up
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GettingListofFiles.aspx.cs" Inherits="GettingListofFiles" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 41%;
        }
        .style2
        {
            font-family: "Californian FB";
            font-size: x-large;
            width: 256px;
        }
        .style3
        {}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    
        <table class="style1">
            <tr>
                <td class="style2">
                    Upload Files</td>
                <td>
                    <asp:FileUpload ID="fufileUpload" runat="server" />
                </td>
            </tr>
            <tr>
                <td class="style3" colspan="2">
                    <asp:Button ID="btnUpload" runat="server" Text="Upload" 
                        onclick="btnUpload_Click1" />
                </td>
            </tr>
        </table>
        <br />
        <asp:GridView ID="xFileList" runat="server">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            </Columns>
        </asp:GridView>

    
    </div>
    </form>
</body>
</html>
// Code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class GettingListofFiles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }

    }
    

    private void BindGridView()
    {
        // getting the directory where the files are stored
        DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("~/FilesUploaded"));
        // getting the info of all the files like when it was uploaded, when modified etc.
        FileInfo[] fileInfo = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);
        xFileList.DataSource = fileInfo;
        xFileList.DataBind();     
    }
    protected void btnUpload_Click1(object sender, EventArgs e)
    {
        try
        {
            string filename = System.IO.Path.GetFileName(fufileUpload.FileName);
            //Setting the path to upload Files
            fufileUpload.SaveAs(Server.MapPath("FilesUploaded/") + filename);
            BindGridView();

        }
        catch (Exception ex)
        {

        }
    }
}





Conclusion


If you see the screen shot we get all the info about the files.This helps the administrator to know when the files were uploaded,modified,created etc.



Page copy protected against web site content infringement by Copyscape

About the Author

Raj.Trivedi
Full Name: Raj Trivedi
Member Level:
Member Status: Member,MVP
Member Since: 6/16/2012 2:04:41 AM
Country: India
Regard's Raj.Trivedi "Sharing is Caring" Please mark as answer if your Query is resolved
http://www.dotnetfunda.com/profile/raj.trivedi.aspx
Raj Trivedi i.e. me started my career as Support Professional and then moved on the Software development eventually reached at these skills Software Development | Enthusiastic Blogger | Content Writer | Technical Writer | Problem Solver | Lecturer on Technology Subjects | Runnerup Award Winner on www.dotnetfunda.com and firm believer in Sharing as a way of Caring Yet this much achieved its still a long way to go and there is biggest dream lying to be one of the best entrepreneurs of India in Technology Department. The Dream has just started and i hope it follows. Highlights are mentioned in details in my profile at http://in.linkedin.com/pub/raj-trivedi/30/61/b30/

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)