前言:
單位 |
描述 |
bit |
位. 一個位元據0或1,是1bit |
byte |
位元組: 儲存空間的基礎單位. 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; } }
效果: