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