Y
Y
Yaroslav Yakovlev2022-03-16 20:13:46
C++ / C#
Yaroslav Yakovlev, 2022-03-16 20:13:46

Why doesn't it return what I need C#?

public static string Searcher(string path, string pattern)
        {
            
            var parts = new List<DirectoryInfo> { };
            
            DirectoryInfo dir = new DirectoryInfo(path);
            
            try
            {
                DirectoryInfo[] dirs = dir.GetDirectories();
                foreach (DirectoryInfo di in dirs)
                {
                    
                    parts.Add(di);
                }
            }
            catch { };
            foreach (DirectoryInfo part in parts.ToList())
            {
                
                if(part.ToString().Contains(pattern) == true)
                {

                    return part.ToString();

                }
                else
                {
                    
                    Searcher(part.ToString(), pattern);
                    
                }
                
                
            }
            return null;
        }

Why does the code return None and not part and how to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
oleg_ods, 2022-03-17
@yyakovlev0

If I understand correctly, then the code should return a folder that contains some value (pattern) in the name. If so, then pattern should be looked for not in part.ToString(), but in part.Name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question