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.

7 comments:

  1. Nice Example,please give some other Large Example so that it will be helpful for all the .net people

    with regards,
    santosh

    ReplyDelete
  2. Hi Venkat,
    The 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

    ReplyDelete
  3. i tried this but its giving me error below:
    Column 'FileName' does not belong to table Files.

    ReplyDelete
  4. Hi Venkat,
    so 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

    ReplyDelete
  5. 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.
    thanks and regards
    siyakant chaudhary
    Kathmandu,Nepal

    ReplyDelete
  6. what is {0:d} on date bound column?

    ReplyDelete
  7. {0:Format} --> {0:d} "d" is Format Pattern for short date.

    ReplyDelete

If you are aware of any other asp.net questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us