zoukankan      html  css  js  c++  java
  • 文件大小友好显示类

     public static string GetFriendlySizeStr(string srcPath)
            {
                var size = 0l;
                size = GetDirSizeInBytes(srcPath);
                var unit = 1024;
                var kb = unit;
                if(size<10*kb)
                {
                    return string.Format("{0}Bytes", size);
                }
                var mb=kb* unit;
                if(size<10*mb)
                {
                    return string.Format("{0}.{1}KB", size/kb,size%kb);
                }
                var gb =mb* unit;
                if(size<gb)
                {
                    return string.Format("{0}.{1}MB", size/mb, size%mb/kb);
                }
                return string.Format("{0}GB {1}.{2}MB", size/gb, size/mb, size%mb/kb);
            }
            public static long GetDirSizeInMB(string srcPath)
            {
                return GetDirSizeInKB(srcPath)/1000;
            }
            public static long GetDirSizeInKB(string srcPath)
            {

                return GetDirSizeInBytes(srcPath) / 1000;
            }
            public static long GetDirSizeInBytes(string srcPath)
            {
                var dirSize = 0l;
                try
                {
                    
                    // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
                    string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath);
                    // 遍历所有的文件和目录
                    foreach (string file in fileList)
                    {
                        // 先当作目录处理如果存在这个目录就重新调用GetDirSize(string srcPath)
                        if (System.IO.Directory.Exists(file))
                        { dirSize+= GetDirSizeInBytes(file); }
                        else
                        {   dirSize += GetFileSizeInBytes(file);}
                    }
                }
                catch (Exception e)
                {
                }
                return dirSize;
            }
            public static long GetFileSizeInBytes(string file)
            {
                System.IO.FileInfo fiArr = new System.IO.FileInfo(file);
                return fiArr.Length;
            }
  • 相关阅读:
    启动dr-elephant失败问题
    hive2.3 任务因一个map导致进程oom挂掉的排查
    hive客户端远程debug
    jdk命令行工具
    hadoop联邦集群 Hive 服务不重启udf函数生效
    livy server高并发下报错java.lang.RuntimeException: java.io.IOException: Unable to connect to provided ports 10000~10010
    使用apache livy导致的结果集不一致问题记录
    spark与hive引擎差异致结果集不一致
    mysql终止当前正在执行的sql语句
    Linux 修改文件目录权限
  • 原文地址:https://www.cnblogs.com/jinzhao/p/2411396.html
Copyright © 2011-2022 走看看