The Directory and the DirectoryInfo classes are used to represent a folder in the file system. But there are critical differences between the two. The Directory class has only static methods, and it is used when you need to perform just one operation on a folder. Since you don't have to create an object to represent the directory to perform one operation, all of the methods of the Directory class are static, so you perform the operation without creating an instance of that class
In this article we will be discussing about DirectoryInfo class to list all the directories and files in the directories
The DirectoryInfo class provides information about a given directory which can be set through the constructor while creating the DirectoryInfo object. Once we get a DirectoryInfo object bound to a directory on the file system, we can get any sort of information about it including files it contains. I have included examples of searching for specific files in the directory instead of getting the complete list.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
string folderPath;
protected void Page_Load(object sender, EventArgs e)
{
if(Request.QueryString.HasKeys())
{
folderPath = Request.QueryString["Folder"];
if (folderPath == null || folderPath == "/")
{
folderPath = Request.ApplicationPath.ToString();
}
else if (folderPath.EndsWith("/"))
{
folderPath = folderPath.Substring(0,folderPath.Length-1);
}
printFolderAndFiles();
}
}
public void printFolderAndFiles()
{
string location;
DirectoryInfo parentDir;
DirectoryInfo[] childDirs;
FileInfo[] childFiles;
try
{
parentDir = new DirectoryInfo(folderPath);
childDirs = parentDir.GetDirectories();
childFiles = parentDir.GetFiles();
}
catch(Exception ex)
{
lblMessage.Text = ex.Message;
return;
}
foreach (DirectoryInfo chDir in childDirs)
{
HtmlTableRow rowItem = new HtmlTableRow();
HtmlTableCell cellItemLink = new HtmlTableCell();
HtmlTableCell cellFolderType = new HtmlTableCell();
location = folderPath;
if (location.EndsWith("/"))
{
location += chDir.Name;
}
else
{
location += "/" + chDir.Name;
}
cellItemLink.Controls.Add(new LiteralControl("<a href='Default.aspx?Folder=" + location + "' style='font-family:verdana;font-size:9pt;'> <img src='./Images/folder.gif' border='0'>" + chDir.Name + "</a>"));
rowItem.Cells.Insert(0, cellItemLink);
folderandfiles.Rows.Add(rowItem);
foreach (FileInfo chFil in childFiles)
{
HtmlTableRow rowItemFile = new HtmlTableRow();
HtmlTableCell cellItemLink1 = new HtmlTableCell();
location = folderPath;
cellItemLink1.Controls.Add(new LiteralControl("<a href='#'><img src='./Images/file.bmp' border='0' font-family:verdana;font-size:9pt;'>" + chFil.Name + "</a>"));
System.IO.FileInfo file = new FileInfo(Server.MapPat(chFil.Name));
rowItemFile.Cells.Insert(0, cellItemLink1);
folderandfiles.Rows.Add(rowItemFile);
}
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
folderPath = txtfolder.Text;
if (folderPath == null || folderPath == "/")
{
folderPath = Request.ApplicationPath.ToString();
}
else if(folderPath.EndsWith("/"))
{
folderPath = folderPath.Substring(0, folderPath.Length - 1);
}
printFolderAndFiles();
}
}