码迷,mamicode.com
首页 > 其他好文 > 详细

递归遍历指定目录,获取该目录下最大的文件信息

时间:2016-02-02 16:17:21      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            FileInfo maxFile = GetMaxFileInfo(new DirectoryInfo(@"e:\root\"), null);
            if (maxFile != null)
            {
                using (FileStream fsRead = new FileStream(maxFile.FullName, FileMode.Open))
                {
                    long fsLen = fsRead.Length;
                    byte[] heByte = new byte[fsLen];
                    int r = fsRead.Read(heByte, 0, heByte.Length);
                    string myStr = Encoding.Default.GetString(heByte);
                    Console.WriteLine(myStr);
                }
            }
            Console.Read();
        }

        public static FileInfo GetMaxFileInfo(FileSystemInfo info, FileInfo maxFile)
        {
            if (info.Exists == false) return null;
            DirectoryInfo dir = info as DirectoryInfo;
            if (dir == null) return null;
            FileSystemInfo[] files = dir.GetFileSystemInfos();
            for (int i = 0; i < files.Length; i++)
            {
                FileInfo file = files[i] as FileInfo;
                if (file == null)
                {
                    maxFile = GetMaxFileInfo(files[i], maxFile);
                }
                else
                {
                    if (maxFile == null)
                    {
                        maxFile = file;
                    }
                    else if (maxFile.Length < file.Length)
                    {
                        maxFile = file;
                    }
                }
            }
            return maxFile;
        }
    }
}

 原题链接:http://www.qlcoder.com/task/755a

递归遍历指定目录,获取该目录下最大的文件信息

标签:

原文地址:http://www.cnblogs.com/tqlin/p/5177725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!