Get all files in Directory and Sub directory using Asp.net
Get all files in Directory and Sub directory using Asp.net
using System.IO;
public void GetFiles(string path)
{
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("File Number " + position.ToString() + ". Path: " + path + "
");
position++;
}
/*To use the methods just call the following method with our base/root directory:
GetFiles("C:\\Test\\");*/
To use the methods just call the following method with our base/root directory:
GetFiles("C:\\Test\\");