In this article I will explain with an example, how to display list of files from Folder (Directory) in ASP.Net using C# and VB.Net.
Files Folder (Directory) Location
The files are located inside the Files Folder (Directory) of ASP.Net project.
HTML Markup
The HTML Markup consists of following control:
GridView – For displaying data.
Columns
The GridView consists of a BoundField column and a TemplateField column.
The TemplateField consists of an ItemTemplate inside which a LinkButton is used.
The LinkButton has been assigned with following property:
CommandArgument – It is set with the path of the file.
The LinkButton has been assigned with the OnClick event handler.
<asp:GridView ID="gvFiles" runat="server" AutoGenerateColumns="false" EmptyDataText="No files uploaded">
<Columns>
<asp:BoundField DataField="Text" HeaderText="File Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" Text="Download" CommandArgument='<%# Eval("Value" )%>' runat="server" OnClick="DownloadFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Web;
VB.Net
Imports System.IO
Imports System.Web
Displaying List of Files from Folder (Directory)
Inside the Page_Load event handler, the path of all files in the Files Folder (Directory) is fetched using the GetFiles method of the Directory class and stored into a String Array.
Then, a Generic List collection of ListItem class object is created.
After that, a FOR EACH loop is executed over the path of the files and the file name and file path is stored into a new ListItem class object as Text and Value respectively.
Finally, the Generic List collection of ListItem class object is assigned to the DataSource property of the GridView and the GridView is populated.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//Fetch all files in the Folder (Directory).
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Files/"));
List<ListItem> files = new List<ListItem>();
//Populating Listof 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
'Fetch all files in the Folder (Directory).
Dim filePaths() As String = Directory.GetFiles(Server.MapPath("~/Files/"))
Dim files As List(Of ListItem) = New List(Of ListItem)
'Populating Listof ListItem class.
For Each filePath As String In filePaths
'Storing the Name and Path in Text and Value properties.
files.Add(New ListItem() With {
.Text = Path.GetFileName(filePath),
.Value = filePath
})
Next
gvFiles.DataSource = files
gvFiles.DataBind()
End If
End Sub
Downloading Files using C# and VB.Net
When the Download LinkButton is clicked, the file path and the file name of the associated file are determined.
Then, the Response class properties are set.
Properties:
1. ContentType – It informs the Browser about the file type.
The file is determined using GetMimeMapping method of MimeMapping class.
2. Content-Disposition – It is a response header indicating, the download file is an attachment and allows setting the file name.
Finally, the file is written to the Response using WriteFile method using file path as parameter and the File is downloaded.
C#
protected void DownloadFile(object sender,EventArgs e)
{
//Fetch the path of the File.
string filePath = (senderas LinkButton).CommandArgument;
//Determine the name of the File.
string fileName = Path.GetFileName(filePath);
//Determine and set the Content Type of the File.
Response.ContentType = MimeMapping.GetMimeMapping(fileName);
//Specifying that the file is an Attachment.
Response.AppendHeader("Content-Disposition","attachment; filename=" + fileName);
//Writing the File to theRespone.
Response.WriteFile(filePath);
Response.End();
}
VB.Net
Protected Sub DownloadFile(ByVal sender As Object, ByVal e As EventArgs)
'Fetch the path of the File.
Dim filePath As String = CType(sender,LinkButton).CommandArgument
'Determine the name of the File.
Dim fileName As String = Path.GetFileName(filePath)
'Determine and set the Content Type of the File.
Response.ContentType = MimeMapping.GetMimeMapping(fileName)
'Specifying that the file is an Attachment.
Response.AppendHeader("Content-Disposition", ("attachment; filename=" + fileName))
'Writing the File to theRespone.
Response.WriteFile(filePath)
Response.End()
End Sub
Screenshot
Downloads