Returns a size string out of a size long for example 10MB. - CSharp System.IO

CSharp examples for System.IO:File Size

Description

Returns a size string out of a size long for example 10MB.

Demo Code

/*//from   w w  w  . j  a va 2  s.c o m
 * $Id: StringUtils.cs 465 2008-10-15 11:53:17Z spocke $
 *
 * Copyright ? 2007, Moxiecode Systems AB, All rights reserved. 
 */
using System.Text.RegularExpressions;
using System.Text;
using System;

public class Main{
        /// <summary>
		///  Returns a size string out of a size long for example 10MB.
		/// </summary>
		/// <param name="size">Size long to convert.</param>
		/// <returns>Size string representation of the specified long.</returns>
 		public static string GetSizeStr(long size) {
			// MB
			if (size > 1048576)
				return Math.Round(size / 1048576.0, 1) + " MB";

			// KB
			if (size > 1024)
				return Math.Round(size / 1024.0, 1) + " KB";

			return size + " bytes";
		}
}

Related Tutorials