日期:2014-05-17  浏览次数:20808 次

c#遍历一个文件夹下的所有文件包括子文件夹(转载)
using System;
using System.IO;

class ListAllFilesDemo
{
    public static void Main()
    {
        Console.Write("请输入要查询的目录:   ");
        string dir = Console.ReadLine();
        try
        {
            ListFiles(new DirectoryInfo(dir));
        }
        catch (IOException e)
        {
            Console.WriteLine(e.Message);
            Console.ReadLine();
        }
    }

    public static void ListFiles(FileSystemInfo info)
    {
        if (!info.Exists) return;

        DirectoryInfo dir = info as DirectoryInfo;
        //不是目录
        if (dir == null) return;

        FileSystemInfo[] files = dir.GetFileSystemInfos();
        for (int i = 0; i < files.Length; i++)
        {
            FileInfo file = files[i] as FileInfo;
            //是文件
            if (file != null)
                Console.WriteLine(file.FullName + "\t " + file.Length);
            //对于子目录,进行递归调用
            else
                ListFiles(files[i]);
        }
    }
}