CSharp examples for System.IO:File Size
Get readable File Size
using System.Text; using System.IO;/*from w ww.jav a2 s . c om*/ using System.Globalization; using System.Collections.Generic; using System; public class Main{ public static string GetFileSize(this System.IO.FileInfo fileinfo) { var byteCount = fileinfo.Length; string size = "0 Bytes"; if (byteCount >= 1073741824.0) size = String.Format("{0:##.##}", byteCount / 1073741824.0) + " GB"; else if (byteCount >= 1048576.0) size = String.Format("{0:##.##}", byteCount / 1048576.0) + " MB"; else if (byteCount >= 1024.0) size = String.Format("{0:##.##}", byteCount / 1024.0) + " KB"; else if (byteCount > 0 && byteCount < 1024.0) size = byteCount.ToString() + " Bytes"; return size; } }