如何检查路径中是否有文件夹并在c#中打开它们

我一直在尝试制作一个控制台应用程序,它会询问你的路径和扩展名,并打印在路径中输入了扩展名的所有文件的名称,但我想让它这样做,如果路径中有更多的文件夹,它将打开文件夹并搜索用户输入的扩展名为的文件。

下面是我的代码:

            Console.WriteLine("What is the path?");
            string path = Console.ReadLine();

            Console.WriteLine("What is the extension?");
            string extension = Console.ReadLine();
            string[] files = System.IO.Directory.GetFiles(path, $"*{extension}");        
            foreach(string file in System.IO.Directory.GetFiles(path,"*.txt"))
            {
                Console.WriteLine(Path.GetFileName(file));   
            }

我的代码只在提供的路径中查找文件,而不是在文件夹中查找和搜索文件

提前感谢

转载请注明出处:http://www.mlhpz.com/article/20230514/914972.html