Get File Size String - CSharp System.IO

CSharp examples for System.IO:File Size

Description

Get File Size String

Demo Code


using System.Text;
using System.IO;/*  w ww  . j  a v  a  2s  . c  o  m*/
using System.Collections.Generic;
using System.Collections;
using System;

public class Main{
        public static string GetFileSizeString(string filename) {
			FileInfo info = new FileInfo(filename);
			// ReSharper disable PossibleLossOfFraction
			double len = info.Length / 1024;
			// ReSharper restore PossibleLossOfFraction
			if (len < 1024) {
				return "" + Parser.ToString((int) (10 * len) / 10.0) + " KB";
			}
			len /= 1024;
			if (len < 1024) {
				return "" + Parser.ToString((int) (10 * len) / 10.0) + " MB";
			}
			len /= 1024;
			return "" + Parser.ToString((int) (10 * len) / 10.0) + " GB";
		}
        public static string ToString(IDictionary d) {
			StringBuilder result = new StringBuilder();
			foreach (string key in d.Keys) {
				result.AppendLine(key + "\t" + d[key]);
			}
			return result.ToString();
		}
}

Related Tutorials