kamagra how much to take

Iterating / Recurring Through Nested Folders in C#

I ran into this issue at a client, and I found the information frickin sparse that was out there. If, at some point, you need to iterate through subfolders in a document library, you will find a nice set of posts out there saying “hey, just use recursion”. Being one that prefers copying and pasting methods into my classes, that just doesn’t really blow my hair back.

So, if you need to iterate through all your subfolders, here is the simple method that you can use to do so. You could obviously make this better with generics, but I am entirely too lazy to do so. You will notice that you should test whether you are returning the “forms” folder, since that is reserved and we don’t really care what is in it.

  1. public SPFolderCollection subFolderCollection = null;
  2.  
  3. private void recurSubFolders(HtmlTextWriter output, string folderPath, SPFolderCollection subFolders)
  4. {
  5. if ((this.subFolderCollection != null) && (this.subFolderCollection.Count > 1))
  6. {
  7. foreach (SPFolder folder in subFolders)
  8. {
  9. if (folder.Name.ToLower() != "forms")
  10. {
  11. string text = folderPath + @"\" + folder.Name;
  12. output.Write(text);
  13. this.recurSubFolders(output, text, folder.SubFolders);
  14. }
  15. }
  16. }
  17. }

That’s it. Happy recurring!

share save 171 16 Iterating / Recurring Through Nested Folders in C#

3 Comments

  1. Sean says:

    This should handle one part of my current conundrum. Thank you.

    Now to schedule automatic uploads from shares to a report library.

  2. Adam Buenz says:

    No problem, glad I could help. I had to do something similar to what it sounds like you are doing, so if you have problems feel free to ping me.

  3. Stieven says:

    Hi,

    This looks like somthing i could use but how do I use this piece of code?

    Thx for a little more explaination for a starter :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>