C#--文件的大小
前言:
单位 描述 bit 位.
一个二进制数据0或1,是1bitbyte 字节:
存储空间的基础单位.
1byte=8bit
b=bit 表示“位”
B=Byte 表示“字节”
代码:
public class FileSize { DirectoryInfo Dic; public FileSize(string FolderPath) { // //TODO: 在此处添加构造函数逻辑 // Dic = new DirectoryInfo(FolderPath); } public void ListFileSize() { FileInfo[] files = Dic.GetFiles(); if (files.Length > 0) { HttpResponse response = HttpContext.Current.Response; response.Write("<table style='border:solid 1px black;border-collapse:collapse;'>"); foreach (FileInfo fi in files) { response.Write("<tr>"); response.Write("<td style='border:solid 1px black'>" + fi.Name+"</td>"); response.Write("<td style='border:solid 1px black'>" + CalculateSize(fi.Length) + "</td>"); response.Write("</tr>"); } response.Write("</table>"); } } private string CalculateSize(long size) { string length = string.Empty; if (size < 1024) { length = size + "bytes"; } else if (size < 1024 * 1024) { length = float.Parse((size * 10 / 1024).ToString()) / 10 + "KB"; } else if (size < 1024 * 1024 * 1024) { length = float.Parse((size * 10 / 1048576).ToString()) / 10 + "MB"; } else { length = float.Parse((size * 10 / 1073741824).ToString()) / 10 + "GB"; } return length; } }
效果: