Get all files in Directory and Sub directory using Asp.net is easy task we have to use this name space using System.IO;
And Call the GetFiles() function to get all files from the directory we use MapPath(“.”) to get the root folder and show all files in side it. If you want to show a perticular folder’s file then change (“.”) to (“foldername”) then it will show all the files in side the folder.
public void GetFiles()
{
string path = MapPath(".");
if (File.Exists(path))
{
// File path
ProcessFile(path);
}
else if (Directory.Exists(path))
{
// This path is a directory
ProcessDirectory(path);
}
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
public void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries)
ProcessFile(fileName);
// Recurse into subdirectories of this directory.
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}
// Insert logic for processing found files here.
public void ProcessFile(string path)
{
FileInfo fi = new FileInfo(path);
Response.Write("Path: " + path + "<br/>");
}
The above GetFiles() method print this
Path: H:\CATEGORY WISE CODE LIBRARY ASP.NET\SEO\ASP\About.aspx
Path: H:\CATEGORY WISE CODE LIBRARY ASP.NET\SEO\ASP\About.aspx.cs
Path: H:\CATEGORY WISE CODE LIBRARY ASP.NET\SEO\ASP\About.aspx.designer.cs
Path: H:\CATEGORY WISE CODE LIBRARY ASP.NET\SEO\Contact.aspx.cs
Path: H:\CATEGORY WISE CODE LIBRARY ASP.NET\SEO\Contact.aspx.designer.cs
Path: H:\CATEGORY WISE CODE LIBRARY ASP.NET\SEO\Default.aspx
Path: H:\CATEGORY WISE CODE LIBRARY ASP.NET\SEO\Default.aspx.cs