0
using System;
using System.IO;

namespace list_of_files
{
    class Program
    {

        private static void FilesandFolders(string Path1, int Level = 0)
        {
            string[] directory = Directory.GetDirectories(Path1);
            string[] set = Directory.GetFiles(Path1);

            try
            {
                foreach (string file in set)
                {
                    Console.WriteLine("└" + Path.GetFileNameWithoutExtension(file));
                }

                foreach (string file in directory)
                {
                    Console.WriteLine("+" + Path.GetFileNameWithoutExtension(file));
                    FilesandFolders(file, Level++);
                }
            }
            catch { }
        }

        static void Main()
        {
            Console.WriteLine("Программа выводит каталог папок и файлов в заданной папке.");
            Console.Write("Введите имя папки: ");
            string path = Console.ReadLine();
            Console.Read();
        }
    }
}

2 ответа 2

1
class Program
{

    private static void FilesandFolders(string Path1, int down = 0)
    {
        string[] directory = Directory.GetDirectories(Path1);
        string[] set = Directory.GetFiles(Path1);
        try
        {
            foreach (string file in set)
            {
                Console.WriteLine(new string(' ', down) + "   └ " +  Path.GetFileNameWithoutExtension(file));
            }
            foreach (string file in directory)
            {
                Console.WriteLine(new string(' ', down) + "   + " +  Path.GetFileNameWithoutExtension(file));
                FilesandFolders(file, down++);
            }
        }
        catch { }
    }
    static void Main()
    {
        Console.WriteLine("Программа выводит каталог папок и файлов в заданной папке.");
        Console.Write("Введите имя папки: ");
        string path = Console.ReadLine();
        Console.WriteLine("   " + path);
        FilesandFolders(path, 0);
        Console.Read();
    }
1

ну вот где то так, просто вложил цикл в другой и все:

    private static void SearchFoldersAndFiles(String path, DirectoryInfo parent = null, String space = null)
    {
        DirectoryInfo directoryInfo = new DirectoryInfo(path);

        foreach (var item in directoryInfo.GetDirectories())
        {
            Console.WriteLine($"\t{space}+{item.Name}");
            SearchFoldersAndFiles(path + @"\" + item.Name, item.Parent, "\t");
            foreach (var file in item.GetFiles())
            {
                Console.WriteLine($"\t\t└{file.Name}");
            }
            Console.WriteLine();
        }
    }

    static void Main(string[] args)
    {
        Console.WriteLine("Программа выводит каталог папок и файлов в заданной папке.");
        Console.Write("Введите имя папки: ");
        string path = Console.ReadLine();
        Console.WriteLine("   " + path);
        SearchFoldersAndFiles($@"{path}", null);
        Console.ReadKey();
    }

P.S. можно еще поиграться с выводом)

2
  • Мне кажется будет лучше сделать два отдельных цикла. Посмотрите код выше, мб замечания появятся, да и буду благодарен, если предложите иной способ решения проблемы.
    – K.Vlad
    16 дек 2018 в 12:49
  • может и есть в этом логик но мне не нравится она)
    – Yaroslav
    16 дек 2018 в 15:40

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.