如何使用.NET获得可读的文件大小(以字节缩写为单位)?
示例 :输入7,326,629并显示6.98 MB
这不是最有效的方法,但是如果您不熟悉日志数学,则更容易阅读,并且对于大多数情况来说应该足够快。
string[] sizes = { "B", "KB", "MB", "GB", "TB" }; double len = new FileInfo(filename).Length; int order = 0; while (len >= 1024 && order < sizes.Length - 1) { order++; len = len/1024; } // Adjust the format string to your preferences. For example "{0:0.#}{1}" would // show a single decimal place, and no space. string result = String.Format("{0:0.##} {1}", len, sizes[order]);