0

I've got a method that recurses over a directory and builds a Tree:

public void RecurseFolders(TreeNode mainNode) {
  DirectoryInfo nodeDir = new DirectoryInfo(mainNode.Tag.ToString());
  try {
    foreach (var dir in nodeDir.GetDirectories()) {
      int index = GetSystemIcon(dir.FullName, treeView1.ImageList, false);
      var subNode = new TreeNode(dir.Name, index, index);
      subNode.Tag = dir.FullName;
      mainNode.Nodes.Add(subNode);
      RecurseFolders(subNode);
    }
  } catch (UnauthorizedAccessException err) {
    Console.WriteLine(err);
  }
}

What I'd like to do with this is find a way to write a Parallel.ForEach out of it, but my LINQ knowledge is too virgin.

Obviously, I can't pass the TreeNode into the thread, so I modified the signature to be more generic. This is as far as I got:

public string[] RecurseFolders(string dirString) {
  List<string> list = new List<string>();
  DirectoryInfo nodeDir = new DirectoryInfo(dirString);
  Parallel.ForEach(nodeDir.GetDirectories(), dir => {
    // how do I write this?
  });
  return list.ToArray();
}

How would I finish it?

EDIT:

This is pulling a list of directories and files off of our network storage drive. Getting the information across the network is currently our bottleneck, but it is a good place for me to learn some Parallel Processing techniques.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
  • 5
    Just a note, depending on the filesystem you're doing this against, it's likely not going to speed up and might even slow down your application. – Yuriy Faktorovich Jun 30 '12 at 03:07
  • If you are using Recursion there are chances that you encounter Stack Overflow exception when parsing large sized drives – techno Jun 30 '12 at 04:13
  • 1
    @techno: How deep do you think you have to recurse to overflow the stack? – Gabe Jun 30 '12 at 04:22
  • @Gabe I have tried to create a File Name Fetcher myself using Recursion and after trying to fetch the C: drive i have encountered SO Exception – techno Jun 30 '12 at 04:23
  • @techno: I've managed to recurse thousands of levels deep in C#. You can't have a filesystem that deep. – Gabe Jun 30 '12 at 04:36
  • @Gabe See what i have done http://stackoverflow.com/questions/11166990/system-stackoverflow-exception-while-parsing-through-directory-structure im i doing something incorrect? – techno Jun 30 '12 at 04:41
  • @techno: I couldn't create a directory tree that I can navigate in C# that's more than 122 levels deep, and the code from your question has no problem getting that deep. – Gabe Jun 30 '12 at 05:34
  • @Gabe The Call Stack will overflow at some point or other.So recursion is not recommended. – techno Jun 30 '12 at 06:38
  • You shouldn't reach enough levels deep to overflow the stack for a normal folder structure. You will get a stack overflow if there are reparse/junction points in an NTFS system that cause infinitely deep folder structures. For example, if you mount your C: drive as a folder within the same C: drive. To test for this scenario, go to Disk Management, R-click C:, "Change drive letters and paths", click Add, "Mount in the following empty NTFS folder" and choose an empty folder in C:. You fix this problem by checking each folder to ensure it is not a reparse point during recursion. – BlackWasp Jul 21 '12 at 09:37

1 Answers1

0

But you already did it!

    public string[] RecurseFolders(string dirString) {
      List<string> list = new List<string>();
      DirectoryInfo nodeDir = new DirectoryInfo(dirString);
      Parallel.ForEach(nodeDir.GetDirectories(), dir => {
//Just continue writing here. This is an Action. Google it for more info. But for the purposes of this example you may consider it as method which will be called for each of the items
      });
      return list.ToArray();
    }
Nas
  • 1,063
  • 2
  • 9
  • 22
  • I'm not sure how to write the Action that returns string values. When I get the string values, they are going to be all out of order, too. Correct? –  Jun 30 '12 at 18:45
  • Just remember to check for reparse points, as per my comment on the question. – BlackWasp Jul 21 '12 at 09:38