简体   繁体   English

使用递归和string.split分割目录

[英]Split directories using recursion and string.split

Here's my problem. 这是我的问题。 I'm using C# and I'm supposed to design a program that runs a directory and list all of the sub-directories and files within. 我正在使用C#,并且应该设计一个运行目录的程序,并列出其中的所有子目录和文件。 I've got the basic code down, which is shown below: 我记下了基本代码,如下所示:

namespace DirectorySearch
{
    class DirectorySearch
    {
        const string PATH = @"C:\Directory Search";

        static void Main(string[] args)
        {
            DirSearch(PATH);
        }

        static void DirSearch(string dir)
        {
            try
            {
                foreach (string f in Directory.GetFiles(dir))
                    Console.WriteLine(f);
                foreach (string d in Directory.GetDirectories(dir))
                {
                    Console.WriteLine(d);
                    DirSearch(d);
                }

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

The problem I'm having is I'm supposed to use stringName.Split('/').Last so the output is all in one straight line, but I have no idea where to place it in the code. 我遇到的问题是我应该使用stringName.Split('/')。最后,输出全部成一条直线,但是我不知道将其放置在代码中的什么位置。 The output is supposed to look like: 输出应该看起来像:

C:\Directory Search
  File1.txt
  File2.txt
    Folder1
    Folder2
      File3.txt
    Folder3

and so on... I'm pretty sure I can figure out how to make the tabs work after I figure out how to split them up. 依此类推...我很确定我可以弄清楚如何将它们分开后才能弄清楚这些选项卡的工作方式。 Any help would be greatly appreciated. 任何帮助将不胜感激。

You just need to use Path.GetFilename which returns directory name (for directories) or file name with extension 您只需要使用Path.GetFilename返回目录名称(对于目录)或带有扩展名的文件名

foreach (string f in Directory.GetFiles(dir))
        Console.WriteLine(Path.GetFileName(f));

foreach (string d in Directory.GetDirectories(dir))
{
     Console.WriteLine(Path.GetFileName(d));
     DirSearch(d);
}

尝试这个

Console.WriteLine(f.Split('\\').Last());

I'm Not sure if you'r looking for something like this, but you can try this code 我不确定您是否正在寻找这样的东西,但是您可以尝试以下代码

namespace DirectorySearch
{
    class DirectorySearch
    {
        const string PATH = @"C:\Directory Search";

        static void Main(string[] args)
        {
            Console.WriteLine(PATH);
            DirSearch(PATH);
        }

        static void DirSearch(string dir, int depth = 1)
        {
            try
            {
                string indent = new string('\t', depth);
                foreach (string f in Directory.GetFiles(dir))
                    Console.WriteLine(indent + f.Split('\\').Last());//using split
                    //Console.WriteLine(indent + Path.GetFileName(f));//using Get File name

                foreach (string d in Directory.GetDirectories(dir))
                {
                    Console.WriteLine(indent + d.Split('\\').Last()); //using split
                    //Console.WriteLine(indent + Path.GetFileName(d)); //using Get File name
                    DirSearch(d, depth++);
                }

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

Hope this helps you! 希望这对您有所帮助!

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

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