File Size friendly display class

Source: Internet
Author: User
Public static string GetFriendlySizeStr (string srcPath)
{
Var size = 0l;
Size = GetDirSizeInBytes (srcPath );
Var Units = 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
{

// Obtain the file list of the source Directory, which is an array containing the file and directory path
String [] fileList = System. IO. Directory. GetFileSystemEntries (srcPath );
// Traverse all files and directories
Foreach (string file in fileList)
{
// Process the data as a directory first. If this directory exists, call GetDirSize (string srcPath) again)
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.