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\\");*/


Comments

Author: Srikanth Reddy06 Oct 2009 Member Level: Silver   Points : 1

To use the methods just call the following method with our base/root directory:

GetFiles("C:\\Test\\");

Author: Nilesh Prabhakarrao Jadhav31 Dec 2009 Member Level: Gold   Points : 1

Hi Srikanth Reddy,

It's so good but facing an error at position.
eg. Name Position is not declear

pls suggest...

Thanks
Nilesh

Author: Sudheer Kumar15 Jul 2010 Member Level: Silver   Points : 0

Thank you very much for providing this code..

Author: sugandha12 Jan 2011 Member Level: Gold   Points : 0

Nice work..



  • 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: