Retrieve Directory Files


In this article I'm trying to explain how to retrieve files from directory, as per user enter directory path what are the available files in that particular directory we get all the files. Hope this article will help you for fresher’s those who are working in the same domain.

Retrieve Directory Files:



In this article I'm trying to explain how to retrieve files from directory, as per user enter directory path what are the available files in that particular directory we get all the files. Hope this article will help you for fresher’s those who are working in the same domain.

Follow below steps to achieve your goal.

Step-1:


Create a project and then right click on solution explorer then choose Add? NewItem then give a name for that as " RetrieveDirFiles.aspx".

Step-2:


After page creation open source view then simply drag and drop 1 label control, 1 textbox control, 1 button control and 1 listbox control into the design view.

Step-3:


After drag and drop the controls the design page looks like below. See the below source code of that design.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RetrieveDirFiles.aspx.cs" Inherits="ASPnet_RetrieveDirFiles" %>

<!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:Label ID="lblDirFiles" runat="server"
Text="Enter Path and name of Directory :"></asp:Label>
<asp:TextBox ID="txtDirFiles" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnDirFiles" runat="server" onclick="btnDirFiles_Click"
Text="Show files" />
<br />
<br />
<asp:ListBox ID="lstDirFiles" runat="server" Height="193px" Width="790px">
</asp:ListBox>

</div>
</form>
</body>
</html>



Step-4:


Once design has been completed then double click on the button control then wrote below lines of code under button click event.

protected void btnDirFiles_Click(object sender, EventArgs e)
{
//get path entered by user
string path = txtDirFiles.Text;


System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);

//check that if the directory entered by user is available or not
if (dir.Exists)
{
//Get available files from that particular directory
System.IO.FileInfo[] files = dir.GetFiles();

//Read all files and display in to listbox control.

foreach (System.IO.FileInfo file in files)
{
lstDirFiles.Items.Add("Name : " + file.Name + ", Size : " + file.Length.ToString() + ", Last Access Time : " + file.LastAccessTime);
}
}
else
{
lstDirFiles.Items.Add("Directory at " + path + " doesn't exists");
}
}


Step-5:


Now, execute the above code by pressing F5 on keyboard and the see the output.

1

Code Behind:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ASPnet_RetrieveDirFiles : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnDirFiles_Click(object sender, EventArgs e)
{
string path = txtDirFiles.Text;
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
if (dir.Exists)
{
System.IO.FileInfo[] files = dir.GetFiles();
foreach (System.IO.FileInfo file in files)
{
lstDirFiles.Items.Add("Name : " + file.Name + ", Size : " + file.Length.ToString() + ", Last Access Time : " + file.LastAccessTime);
}
}
else
{
lstDirFiles.Items.Add("Directory at " + path + " doesn't exists");
}
}
}



Conclusion:


I Hope this article will help you to retrieve files from directory, as per user enter directory path what are the available files in that particular directory we get all the files. Hope this article will help you for fresher's those who are working in the same domain.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: