Get readable File Size - CSharp System.IO

CSharp examples for System.IO:File Size

Description

Get readable File Size

Demo Code


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

Related Tutorials