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