Get All files of Directories and SubDirectories - C#

Posted by Venkat | Labels: , ,

Here i have to discuss about how to get all the files of Directories and SubDirectories.

Generally we know how to get the files from From directory

ex: Directory.GetFiles("path");

C# Code:


using System.IO;

private int position = 1;

public void GetFiles(string path)

{

if (File.Exists(path))

{

// if file Exists

ProcessFile(path);

}

else if (Directory.Exists(path))

{

// if Directory exists

ProcessDirectory(path);

}

}

public void ProcessDirectory(string targetDirectory)

{

string[] fileEntries = Directory.GetFiles(targetDirectory);

foreach (string fileName in fileEntries)

ProcessFile(fileName);

string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);

foreach (string subdirectory in subdirectoryEntries)

ProcessDirectory(subdirectory);

}

public void ProcessFile(string path)

{

FileInfo fi = new FileInfo(path);

Response.Write("File Number " + position.ToString() + ". Path: " + path + "
"
);

position++;

}


Give File Path like this


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

0 comments:

Post a Comment

Thanks for the Comments.

PayOffers.in