zoukankan      html  css  js  c++  java
  • 递归遍历指定目录,获取该目录下最大的文件信息

    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:
    oot"), 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;
            }
        }
    }
  • 相关阅读:
    MATLAB 2019a 安装包及安装教程
    三角形最大周长
    两数的和
    “精致”的数
    总分最高的学生姓名和各科成绩
    列表元素改写
    统计单词个数
    凯撒密码
    Django入门学习--配置路由(urls)
    Django入门学习--熟悉配置信息
  • 原文地址:https://www.cnblogs.com/tqlin/p/5177725.html
Copyright © 2011-2022 走看看