In this article I will explain with an example, how to display list of files from Server’s Folder (Directory) in ASP.Net GridView using C# and VB.Net.
 
 

HTML Markup

The HTML Markup consists of following controls:
FileUpload – For selecting file.
Button – For uploading selected file.
GridView – For displaying data.

Columns

The GridView consists of following column:
BoundField – For displaying name of the File.
<asp:FileUpload ID="fuUpload" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="UploadFile" />
<hr />
<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="false" EmptyDataText="No files uploaded">
    <Columns>
        <asp:BoundField DataField="Text" HeaderText="File Name" />
    </Columns>
</asp:GridView>
 
 

Namespaces

You will need to import the following namespace.
C#
using System.IO;
 
VB.Net
Imports System.IO
 
 

Uploading the File and saving in Folder (Directory) on Server’s Disk

When the Upload Button is clicked, the uploaded File is saved into the Folder (Directory) and the Page is redirected to itself in order to display the uploaded file in the GridView.
C#
protected void UploadFile(object sender, EventArgs e)
{
    //Fetch the name of the File.
    string fileName = Path.GetFileName(fuUpload.PostedFile.FileName);
 
    //Save the File.
    fuUpload.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
 
    //Redirect to the same page.
    Response.Redirect(Request.Url.AbsoluteUri);
}
 
VB.Net
Protected Sub UploadFile(ByVal sender As Object, ByVal e As EventArgs)
    'Fetch the name of the File.
    Dim fileName As String = Path.GetFileName(fuUpload.PostedFile.FileName)
 
    'Save the File.
    fuUpload.PostedFile.SaveAs((Server.MapPath("~/Uploads/") + fileName))
 
    'Redirect to the same page.
    Response.Redirect(Request.Url.AbsoluteUri)
End Sub
 
 

Displaying the Files from Folder (Directory) in ASP.Net GridView

Inside the Page_Load event handler, the path of all the Files in the Uploads folder is fetched into a String Array using the GetFiles method of the Directory class.
Then, a FOR EACH loop is executed over the Array of file paths and inside the loop, the name path of the File is stored in the Text and Value property of the ListItem class object which is later added to the Generic List collection of ListItem class.
Finally, Generic List collection is assigned to the DataSource property of the GridView control.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        //Fetching the List of Files.
        string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
        List<ListItem> files = new List<ListItem>();
 
        //Populating List of ListItem class.
        foreach (string filePath in filePaths)
        {
            //Storing the Name and Path in Text and Value properties.
            files.Add(new ListItem()
            {
                Text = Path.GetFileName(filePath),
                Value = filePath
            });
        }
 
        gvFiles.DataSource = files;
        gvFiles.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        'Fetching the List of Files.
        Dim filePaths() As String = Directory.GetFiles(Server.MapPath("~/Uploads/"))
        Dim files As List(Of ListItem) = New List(Of ListItem)
 
        'Populating List of ListItem class.
        For Each filePath As String In filePaths
            files.Add(New ListItem() With {
            .Text = Path.GetFileName(filePath),
            .Value = filePath
        })
        Next
        gvFiles.DataSource = files
        gvFiles.DataBind()
    End If
End Sub
 
 

Screenshot

Display list of files from Server folder in ASP.Net GridView
 
 

Demo

 
 

Downloads