List out the .pdf files from the directory and display in ASP.NET Gridview
Here, today we will look into the topic listing out the files from the specified files and displaying those all files in a grid view. We can sort out the files based on the file extensions like .PDF,.doc..etc. Find list out the .pdf files from the directory and display in ASP.NET Gridview. We can list out the .pdf files info along with file name, created date, modified date, length of the file etc. from the directory and display in ASP.NET Gridview whatever the details you want to show in a grid view.
Learn the code for Listing out the .pdf files from the directory and display in ASP.NET Gridview
Here, we are working on the topic listing out the files from the specified files and displaying those all files in a grid view. We can sort out the files based on the file extensions like .PDF,.doc..etc.
In ASP.NET we are having the name space Syatem.IO. This name space contains DirectoryInfo and FileInfo classes to get the list and details about the files exists in a directory.
Below code snippet shows how we are doing this.
C# code:
DirectoryInfo dirInfo = new DirectoryInfo(@"D:\Govind\");
FileInfo[] fileInfo = dirInfo.GetFiles("*.pdf*", SearchOption.AllDirectories);
gvDocs.DataSource = fileInfo;
gvDocs.DataBind();
HTML Part:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DisplayFiles._Default" %>
<!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 >
</head >
<body >
<form id="form1" runat="server" >
<div >
<asp:GridView ID="gvDocs" runat="server" AlternatingRowStyle-BackColor="#eeeeee"
AutoGenerateColumns="false" DataKeyNames="FullName" HeaderStyle-BackColor="navy"
HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="12px" HeaderStyle-ForeColor="white">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="Name" DataTextField="Name" HeaderText="File Name" />
<asp:BoundField DataField="LastWriteTime" HeaderText="LastWriteTime">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
<HeaderStyle BackColor="Navy" Font-Bold="True" Font-Size="12px" ForeColor="White" />
<AlternatingRowStyle BackColor="#EEEEEE" />
</asp:GridView>
</div>
</form>
</body>
</html>