开发者

c# find a FOLDER when path is unknown then list files and return

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);
                Directory开发者_Python百科Info 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
        }
    }


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
  }
}


  • Create a boolean at a global scope.
  • Default it to false.
  • When the folder is found, set it to true.
  • In your recursive function, if the value is true, return and exit from the function without doing anything.

In your case, the foundIt variable is declared and initialized within the function. Declare it at the global level, and check it first thing in the function.

For the exception handling, simply use a try/catch and exit the function if it fails.


You can use the solution from below which doesn't use a global variable.

  public static string FindFolder(DirectoryInfo rootDirectory, string folderToFind, int currentDepth, int maxDepth)
    {
        if(currentDepth == maxDepth)
        {
            return null;
        }

        foreach(var directory in rootDirectory.GetDirectories())
        {
            Console.WriteLine(directory.FullName);

            if(directory.Name.Equals(folderToFind,StringComparison.OrdinalIgnoreCase))
            {
                return directory.FullName;
            }

            string tempFindResult;

            if((tempFindResult = FindFolder(directory,folderToFind,++currentDepth,maxDepth)) != null)
            {
                return tempFindResult;
            }
        }

        return null;
    }


What I think you want to do is break out of the foreach:

if (foundIt == true)
{
    break; // instead of return
}

edit
...right, of course, you need to make foundIt a class member. The current scope applied to each iteration of the method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜