Y
Y
Yaroslav Yakovlev2022-03-09 23:22:08
C++ / C#
Yaroslav Yakovlev, 2022-03-09 23:22:08

I did a method that searches for a folder, but it does not browse the folders to the end, how can I fix this?

There is a folder I need, its path is C:\folder\folder\folder\folder\the folder I
need. But the method does not scan to the end, that is, it displays C:\folder\folder\folder\folder

public static void Searcher(string path,string pattern)
        {
            var parts = new List<DirectoryInfo> {};
           
            DirectoryInfo di = new DirectoryInfo(path);
            DirectoryInfo[] diarr = di.GetDirectories();
            bool isend = false;
            string mypath;
            foreach (DirectoryInfo d in diarr)
            {
                if (d.ToString().Contains(pattern) == true)
                {
                    Console.WriteLine("НАЙДЕНООООООООООООООООООООООООООООООО путь ||" + d);
                    isend = true;
                    mypath = d.ToString();
                }
                
                try
                {
                    diarr = d.GetDirectories();
                }
                catch { }
                parts.Add(d);
                while (isend == false )
                {
                    foreach (DirectoryInfo dir in diarr)
                    {
                        if (dir.ToString().Contains(pattern) == true)
                        {
                            Console.WriteLine("НАЙДЕНООООООООООООООООООООООООООООООО путь ||" + dir);
                            isend = true;
                            mypath = dir.ToString();
                        }
                        
                        try
                        {
                            diarr = dir.GetDirectories();
                            Console.WriteLine(dir);
                        
                        }
                        catch (Exception) { Console.WriteLine("err"); }
                       
                    }
                    if (diarr.Length == 0)
                    {
                        break;
                    }
                }
                if (isend == true)
                {
                    
                    break;
                }





            }
            
            
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2022-03-11
@yyakovlev0

For such tasks, they came up with recursion

функция ПоискПапки(Путь)
   список = СодержимоеПапки(Путь)
   дляКаждогоЗначения(подпуть в список)
         ДобавитьПутьВОбщийСписок(подпуть)
         ПоискПапки(подпуть)
конец

Here is the algorithm how the directory tree is built.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question