List all the files in a directory on a web form in asp.net
Question :
List all the files in a directory on a web form. The files must be displayed in a gridview control. The name of the file and create date must be displayed.
Answer:
1. Create a new web form. Drag and drop a gridview control from the toolbox onto the webform.
2. Create 2 bound fields for the gridview. One bound field will display the file name and the other will display the create date.
3. The HTML for your web form should be as shown below.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListFiles.aspx.cs" Inherits="ListFiles" %>
<html>
<head runat="server">
<title>List all the files in a directory</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="FileName" HeaderText="File Name"></asp:BoundField>
<asp:BoundField DataField="DateCreated" HeaderText="Date" DataFormatString="{0:d}"></asp:BoundField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
4. In the code behind file write a function which can get the list of files from the directory and bind to the gridview. The function is as shown below.
private void LoadFiles()
{
/* Create an instance of DirectoryInfo class for enumarating through the directory. */
System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(Server.MapPath("FilesDirectory"));
/* Call the GetFiles() instance method of the DirectoryInfo class object, which will return a files list from the current directory */
System.IO.FileInfo[] fiFiles = dirInfo.GetFiles();
/* Create a DataTable which can be used as the datasource for the gridview */
DataTable dtFileList = new DataTable("Files");
/* Create a DataColumn for file name */
DataColumn dcFileName = new DataColumn("FileName");
/* Create a DataColumn for file create date */
DataColumn dcDateCreated = new DataColumn("DateCreated", typeof(DateTime));
/* Add the 2 data columns to the data table */
dtFileList.Columns.Add(dcFileName);
dtFileList.Columns.Add(dcDateCreated);
/* Now loop through each FileInfo object and get the file name and file create date */
foreach (System.IO.FileInfo f in fiFiles)
{
DataRow dtNewRow = dtFileList.NewRow();
/* Get the file name using FileInfo object "Name" property */
dtNewRow["FileName"] = f.Name.ToString();
/* Get the file create date and time using FileInfo object "CreationTime" property */
dtNewRow["DateCreated"] = f.CreationTime.ToShortDateString();
/* Add the row to the DataTable */
dtFileList.Rows.Add(dtNewRow);
}
/* Set the datatable as the DataSource for the gridview and call the DataBind() method */
GridView1.DataSource = dtFileList;
GridView1.DataBind();
}
5. Finally call the LoadFiles() method on the page load event handler as shown below.
protected void Page_Load(object sender, EventArgs e)
{
LoadFiles();
}
Testing the application:
1. Right click on the project name in solution explorer, and left click on "NewFolder"
2. Rename the "NewFolder" to "FilesDirectory"
3. Drag and Drop some files into the directoy.
4. Then run the application. All the files in the "FilesDirectory" folder will be shown in the gridview.
Subscribe to:
Post Comments (Atom)
Nice Example,please give some other Large Example so that it will be helpful for all the .net people
ReplyDeletewith regards,
santosh
Hi Venkat,
ReplyDeleteThe real time examples which you had given is so nice...could you please provide some more examples..sothat it would be helpful for .net people.I hope you will provide some more examples.
thanks & Regards,
Nagalakshmi
i tried this but its giving me error below:
ReplyDeleteColumn 'FileName' does not belong to table Files.
Hi Venkat,
ReplyDeleteso nice example and best collection of .net question and his answer. Please write some best notes for sql server also.
Thanks & regards,
Vikas Yadav
Delhi
That's so nice example for increasing knowledge about sql server thats also help for me conneting with visual studio so...thanks for providing that's type of questions and answer.
ReplyDeletethanks and regards
siyakant chaudhary
Kathmandu,Nepal
what is {0:d} on date bound column?
ReplyDelete{0:Format} --> {0:d} "d" is Format Pattern for short date.
ReplyDelete