Listing subdirectories 3 levels deep using LINQ C#

Posted by paradox on Stack Overflow See other posts from Stack Overflow or by paradox
Published on 2010-05-06T08:55:23Z Indexed on 2010/05/06 8:58 UTC
Read the original article Hit count: 451

Filed under:
|

I'd like to know if there is a better alternative to my following code (preferably using LINQ)

            #region List and filter directories to only 3 levels deep
            // List all subdirectories within main directory
            string[] folders = Directory.GetDirectories(@"C:\pdftest\", "*" ,SearchOption.AllDirectories);
            List<string> subdirectories = new List<string>();

            //Filter away all main directories, now we are left with subdirectories 3 levels deep
            for (int i = 0; i<folders.Length; i++)
            {
                int occurences = folders[i].Split('\\').Length-1;
                if (occurences==4)
                    subdirectories.Add(folders[i]);             
            }
            #endregion

© Stack Overflow or respective owner

Related posts about c#

Related posts about subdirectories