Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6674029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:41:51+00:00 2026-05-26T03:41:51+00:00

I am learning c# and need to find a folder when the complete path

  • 0

I am learning c# and need to find a folder when the complete path is unknown. An example would be you know the the album name but not the artist in the music folder. Finding an album name is NOT the final usage for this code but the best example for this question. I am doing this with recursion and limiting the depth of the search. Everything works good except when I find the folder and list the files I want it to stop and return but it does not, it just keeps the recursion going even after I have the the folder. I have also struggled with exception handling like how to skip a folder if permissions are not valid.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

     namespace listFoldersTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.SetWindowSize(100, 50);
                DirectoryInfo dir = new DirectoryInfo(@"C:\Users\username\Music");
                getDirsFiles(dir, 0, 2);
                Console.ReadKey();
                Console.WriteLine("done");
            }
            public static void getDirsFiles(DirectoryInfo d, int currentDepth, int maxDepth)
            {
                String folderToFindName = ("albumName");
                bool foundIt = false;

                if (currentDepth < maxDepth)
                {
                    DirectoryInfo[] dirs = d.GetDirectories("*.*");
                    foreach (DirectoryInfo dir in dirs)
                    {
                        String pathName = (dir.FullName);
                        Console.WriteLine("\r{0} ", dir.Name);
                        if (currentDepth == (maxDepth - 1))
                        {                                                                                                                                         
                            if (pathName.IndexOf(folderToFindName) != -1)
                            {
                                foundIt = true;
                                FileInfo[] files = dir.GetFiles("*.*");
                                foreach (FileInfo file in files)
                                {
                                    Console.WriteLine("-------------------->> {0} ", file.Name);
                                } //end foreach files

                            } // end if pathName

                        } // end if of get files current depth

                        if (foundIt == true)
                        {
                            return;
                        }
                        getDirsFiles(dir, currentDepth + 1, maxDepth);

                    } //end if foreach directories

                } //end if directories current depth

            } // end getDirsFiles function
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:41:52+00:00Added an answer on May 26, 2026 at 3:41 am
    using System;
    using System.IO;
    
    namespace listFoldersTest
    {
      class Program
      {
        private static bool foundIt;
    
        static void Main(string[] args)
        {
            Console.SetWindowSize(100, 50);
            try
            {
                DirectoryInfo dir = new DirectoryInfo(args[0]);
                getDirsFiles(dir, 0, 2);
            }
            catch
            {
            }
            Console.ReadKey();
            Console.WriteLine("done");
        }
    
        public static void getDirsFiles(DirectoryInfo d, int currentDepth, int maxDepth)
        {
            if(d == null || foundIt) return;
            String folderToFindName = ("albumName");
            if (currentDepth < maxDepth)
            {
                DirectoryInfo[] dirs = d.GetDirectories("*.*");
                foreach (DirectoryInfo dir in dirs)
                {
                    String pathName = (dir.FullName);
                    Console.WriteLine("\r{0} ", dir.Name);
                    if (currentDepth == (maxDepth - 1))
                    {
                        if (pathName.IndexOf(folderToFindName) != -1)
                        {
                            foundIt = true;
                            FileInfo[] files = dir.GetFiles("*.*");
                            foreach (FileInfo file in files)
                            {
                                Console.WriteLine("-------------------->> {0} ", file.Name);
                            } //end foreach files
                            return;
                        } // end if pathName
                    } // end if of get files current depth
                    getDirsFiles(dir, currentDepth + 1, maxDepth);
                } //end if foreach directories
            } //end if directories current depth
        } // end getDirsFiles function
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to MSBuild, and am learning as I need to know how to
What do I need to install to begin learning Silverlight 2? I know how
I know how to find what I need from XML using XPath. The syntax
This looks like something simple but I could not find the answer so far
I need to visualize 3D point clouds using C++, I started learning OpenGL but
I'm learning to use ReST Web services and I need to find out how
I'm currently learning C#, but I am stuck on something and I can't find
Learning R language - I know how to do a moving average but I
Learning Android I'm trying to find contacts using DISPLAY_NAME selector. I need to find
I'm learning LINQ to XML and need to find the existence of an element

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.