Get all files in all directories

T

TOI DAY

I need a routine to get all the files in the root dir and all
directories and subdirectories.

For example: I have these directories structure

C:\
Dir1
subdir1
file1.txt
file2.txt
Dir2
Subdir2
subsubdir2
file3.txt
file4.txt
test.doc
test2.doc

and so on

Can anyone help out with the code?

Thanks in advance
 
J

Jon Skeet [C# MVP]

TOI DAY said:
I need a routine to get all the files in the root dir and all
directories and subdirectories.

Right. Use DirectoryInfo.GetFiles and DirectoryInfo.GetDirectories, and
either keep a list of all the directories you still need to search, or
recurse.
 
G

Guest

this code should get you started

public static 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)


ht
jayson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top