C# 遍历目录下所有文件夹下的文件

 

private void FindAllFiles(string fileDirectory)
{
    DirectoryInfo diSource = new DirectoryInfo(fileDirectory);
    FileSystemInfo[] fsi = diSource.GetFileSystemInfos();
    FileInfo fi;

    try
    {
        for (int i = 0; i < fsi.Length; i++)
        {
         //不是目录,查看文件属性;是目录,继续遍历。
            if (Directory.Exists(fsi[i].FullName) == false)
            {
                fi = new FileInfo(fsi[i].FullName);
                //判断文件类型,进行相应的后继操作。
                if (fi.Extension == ".txt")
                {
                 Console.WriteLine(fsi[i].FullName);
                }
            }
            else
            {
                FindAllFiles(fsi[i].FullName);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }

posted @ 2011-12-05 18:36  顿金  阅读(227)  评论(0编辑  收藏  举报