c#查找路径未知的文件夹,然后列出文件并返回



我正在学习c#,需要在完整路径未知时找到一个文件夹。例如,您知道专辑名称,但不知道音乐文件夹中的艺术家。查找专辑名不是这段代码的最终用途,但却是这个问题的最佳示例。我用递归来做这个,并且限制了搜索的深度。一切都很好,除了当我找到文件夹并列出文件时,我想让它停止并返回,但它没有,它只是让递归继续,即使在我找到文件夹之后。我还遇到过异常处理,比如如果权限无效,如何跳过文件夹。

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:UsersusernameMusic");
                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
  }
}
  • 在全局范围内创建布尔值。
  • 默认为false。
  • 当找到文件夹时,将其设置为true。
  • 在你的递归函数中,如果值为true,返回并退出函数,不做任何事情。

在您的示例中,foundIt变量是在函数中声明和初始化的。在全局级别声明它,并在函数中首先检查它。

对于异常处理,只需使用try/catch,如果失败则退出函数。

你可以使用下面不使用全局变量的解决方案。

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

我认为你要做的是foreach中的break:

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

编辑
…对,当然,你需要让foundIt成为类成员。应用于方法每次迭代的当前范围

最新更新