博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件大小友好显示类
阅读量:7157 次
发布时间:2019-06-29

本文共 1242 字,大约阅读时间需要 4 分钟。

 
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;
        }

转载地址:http://fiegl.baihongyu.com/

你可能感兴趣的文章
常用的两种web单点登录SSO的实现原理
查看>>
Elasticsearch小结
查看>>
Android开发小技巧之--------继承AppCompatActivity后设置全屏的问题
查看>>
[转]在vmware中安装64位centOS
查看>>
书荐——《微服务设计》(Sam Newman)
查看>>
PHP数组效率去重
查看>>
Google将要推出一个重新设计的Gmail界面
查看>>
yii2中like的查询
查看>>
gnu nano使用
查看>>
jquery给input框添加只读属性
查看>>
Ajax - Ajax, json, google maps api 遍历
查看>>
算法。
查看>>
Flex布局
查看>>
CAS单点登陆proxy代理实现
查看>>
由Android屏幕旋转说起
查看>>
2.3 Java的数组
查看>>
ubuntu 11.10 安装systemtap
查看>>
Django学习笔记(4)---ManyToMany 添加、删除关联、查询
查看>>
ORACLE----sql优化
查看>>
MyBatis3:SQL映射
查看>>