简体   繁体   English

C#System.IO如何找到给定文件夹的所有子文件夹?

[英]c# System.IO how do i find all the subfolders given a certain folder?

This is what I've done so far: 到目前为止,这是我所做的:

class Program
{

    static void Main(string[] args)
    {

        DirectoryInfo startDirectory = new DirectoryInfo(@"C:\Users\Angelo\Desktop\ExerciseTest");

        string output = "";

        DirectoryInfo[] subDirectoryes = startDirectory.GetDirectories();

        for (int i = 0; i < subDirectoryes.Length; i++)
        {
            output = output + subDirectoryes[i].ToString() + "\r\n";
        }

        Console.WriteLine(output);

        Console.ReadLine();

    }
}

This gives me as a result the first subfolders of the specified folder, the problem is that I need to find all the subfolders,subsubfolders,subsubsubfolders etc.. and files in the specified folder, and then output them with this indentation: 结果,这给了我指定文件夹的第一个子文件夹,问题是我需要找到指定文件夹中的所有子文件夹,子子文件夹,子子文件夹等。以及文件,然后使用此缩进输出:

  • Subfolder1 子文件夹1
    • SubSubfolder1 子文件夹1
      • SubSubSubfolder1 子子子文件夹1
      • SubSubfolderFile1 SubSubfolderFile1
    • SubSubfolder2 子子文件夹2
  • Subfolder2 子文件夹2
    • SubfolderFile1 子文件夹文件1

I've been trying to do it many times but I can't figure out how, maybe I'm missing some command (this is the first time I program with c# System.IO) can you please give me some tips or tell me what commands I should use? 我已经尝试了很多次,但是我不知道怎么做,也许我错过了一些命令(这​​是我第一次使用c#System.IO编程),能否请给我一些提示或告诉我我应该使用什么命令? I'm going crazy. 我要疯了。

There is an overload of GetDirectories you should use that allows you to define the search scope: 应该使用GetDirectories的重载,以允许您定义搜索范围:

DirectoryInfo[] subDirectoryes = 
              startDirectory.GetDirectories("*", SearchOption.AllDirectories);

This will get all directories, including all the subdirectories all the way down. 这将获取所有目录,包括所有子目录。

The first argument is a search text filter, "*" will find all folders. 第一个参数是搜索文本过滤器,“ *”将找到所有文件夹。

A complete example with indentation: 带有缩进的完整示例:

void Main()
{
    var path = @"C:\Users\Angelo\Desktop\ExerciseTest";
    var initialDepth = path.Split('\\').Count();

    DirectoryInfo startDirectory = new DirectoryInfo(path);

    StringBuilder sb = new StringBuilder();

    DirectoryInfo[] subDirectoryes = startDirectory.GetDirectories("*", SearchOption.AllDirectories);

    for (int i = 0; i < subDirectoryes.Length; i++)
    {
        var level = subDirectoryes[i].FullName.Split('\\').Count() - initialDepth;
        sb.AppendLine($"{new string('\t', level)}{subDirectoryes[i].Name}");
    }

    Console.WriteLine(sb.ToString());

    Console.ReadLine();
}

In order to go through all the subfolders, you need recursive function. 为了遍历所有子文件夹,您需要递归函数。 Basically, you have to repeat the procedure that you apply currently only for root directory to all the directories that you encounter. 基本上,您必须对遇到的所有目录重复当前仅对根目录应用的过程。

Edit: Had to take a little while to provide this code example: 编辑:不得不花一点时间来提供此代码示例:

class Program
{
    static void Main(string[] args)
    {
        var basePath = @"C:\Users\Angelo\Desktop\ExerciseTest";

        DirectoryInfo startDirectory = new DirectoryInfo(basePath);
        IterateDirectory(startDirectory, 0);

        Console.ReadLine();
    }

    private static void IterateDirectory(DirectoryInfo info, int level)
    {
         var indent = new string('\t', level);
         Console.WriteLine(indent + info.Name);
         var subDirectories = info.GetDirectories();

         foreach(var subDir in subDirectories)
         {
             IterateDirectory(subDir, level + 1);
         }
     }
 }

A bit of an explanation, what the IterateDirectory does (as requested): 解释一下,IterateDirectory的功能(根据要求):

  1. It prints the directory name with indentation, which is dependent on current level in the directory subtree. 它打印带有缩进的目录名称,该缩进取决于目录子树中的当前级别。
  2. For each of the directories in current directory: call the IterateDirectory method with level increased by one. 对于当前目录中的每个目录:调用IterateDirectory方法,将level提高1。

This is a rather standard approach for going through tree-like structures. 这是遍历树状结构的相当标准的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM