C#查找所有文件名和文件夹名递归算法的实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
 
namespace FindAllFiles
{
    class Program
    {
        public static void FindAllFile(string path)
        {
            
            try
            {
                if (path == "" || !Directory.Exists(path))
                {
                    Console.WriteLine("输入的文件夹路径无效!");
                    return;
                }
                string[]files = Directory.GetFiles(path);
                if (files.Length > 0)
                {
 
                    Console.WriteLine("Files:");
                    foreach (string f in files)
                    {
                        FileInfo file = new FileInfo(f);
                        Console.WriteLine(file.Name);
 
                    }
                }
                string[] dirs = Directory.GetDirectories(path);
                if (dirs.Length > 0)
                {
                    Console.WriteLine("Directory:");
                    foreach (string dir in dirs)
                    {
                        DirectoryInfo d = new DirectoryInfo(dir);
                        Console.WriteLine(d.Name);
                    }
                }           
                if (!(files.Length == 0 && dirs.Length == 0))
                {
                        Console.WriteLine("*********************************************************");
 
                 }
                foreach (string dir in dirs)
                {
                        FindAllFile(dir);
                }
                               
 
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
 
        }
        static void Main(string[] args)
        {
           // string path = Console.ReadLine();
            FindAllFile(@"C:\Users\MrGan\Desktop\Task\EditCell");
            Console.ReadLine();
 
 
 
        }
    }
}
posted @ 2013-04-14 20:33  Predator  阅读(489)  评论(0编辑  收藏  举报