0

I wrote a method that needed to find all files within a path, and I want to get all the files using recursion. Here's my current method:

public void doStart(DirectoryInfo dir, string filePattern) { try { foreach (FileInfo fileInfo in dir.GetFiles(filePattern)) { if (fileFound != null) { fileFound(fileInfo); } } } catch (Exception) { } try { foreach (DirectoryInfo dirInfo in dir.GetDirectories()) { doStart(dirInfo, filePattern); } } catch (Exception) { } } public void Start(string path, string filePattern) { doStart(new DirectoryInfo(path), filePattern); } 

Is there is better way to write this kind of recursion or is this good enough ?

3 Answers 3

7

Try something like this:

string[] filePaths = Directory.GetFiles(@dir, "*.filetype", SearchOption.AllDirectories); 

This would recursively look through the directory, finding all files with a certain filetype ('.filetype') and returns a string array containing all found files.

Also, I'd recommend not to use empty catch blocks, as your application won't let you know if something went wrong. Either show a message box (or something similar) or log it to a database or something.

Further, what would your DoStart() method do if there is a subdirectory in a subdirectory? From what I'm seeing, I'd say it only searches on 1 sublevel.

Sign up to request clarification or add additional context in comments.

14 Comments

The code searches all the subdirectories in this code: foreach (DirectoryInfo dirInfo in dir.GetDirectories()) { doStart(dirInfo, filePattern); }
@NikolaDavidovic According to MSDN, it doesn't ( msdn.microsoft.com/en-us/library/s7xk2b58.aspx ), it only gets the subdirectories for the current directory.
This works like a charm and it's really fast.. as long as you don't set the @Dir to be "c:\\" but other than that it's nice code StefanM
@DJKRAZE I tend to use '@' when using filepaths, because I always forget to escape the '\'. I'm not sure which one of these would be faster, but I don't think there's a huge difference in speed between those options, if any.
Your answer is spot on.. I was just testing it and tried @"c:\" for example and it threw an exception there were certain folders that I didn't have access to..
|
1
  1. Don't swallow all exceptions. If you need to ignore specific exceptions, catch those but let others bubble up
  2. (style) Methods should be PascalCased (e.g. DoStart and `FileFound'
  3. (style) Create an OnFileFound method instead of calling FileFound directly (I assume fileFound is an event handler?)

Other than that it looks fine to me.

Comments

0

Here is an example of true recursion. This will search until there are no more sub-directories to find, unlike Directory.GetFiles SearchOption.AllDirectories. You can modify this to add search filters as a parameter.

public IEnumerable<string> GetFilesRecursive(string ParentDirectory) { string[] subDirectories = Directory.GetDirectories(ParentDirectory); foreach (string file in Directory.GetFiles(ParentDirectory)) { yield return file; } foreach (string subDirectory in subDirectories) { foreach (string file in GetFilesRecursive(subDirectory)) { yield return file; } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.