Returns text of the file size. - CSharp System.IO

CSharp examples for System.IO:File Size

Description

Returns text of the file size.

Demo Code

/********************************************************************
 *  FulcrumWeb RAD Framework - Fulcrum of your business             *
 *  Copyright (c) 2002-2010 FulcrumWeb, ALL RIGHTS RESERVED         *
 *                                                                  *
 *  THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      *
 *  FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        *
 *  COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       *
 *  AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  *
 *  AND PERMISSION FROM FULCRUMWEB. CONSULT THE END USER LICENSE    *
 *  AGREEMENT FOR INFORMATION ON ADDITIONAL RESTRICTIONS.           *
 ********************************************************************/
using System.Collections;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System;/*from ww  w.  j av  a2  s .  c om*/

public class Main{
        //-------------------------------------------------------------------------
    /// <summary>
    /// Returns text of the file size.
    /// </summary>
    /// <param name="dataSize">file size</param>
    /// <returns>display text</returns>
    static public string GetDataSizeText(long dataSize)
    {
      if (dataSize >= 0 && dataSize < 1024)
      {
        return dataSize + " B";
      }
      else if (dataSize >= 1024 && dataSize < 1048576)
      {
        return (((double) dataSize) / 1024).ToString("F2") + " KB";
      }
      else if (dataSize >= 1048576 && dataSize < 1073741824)
      {
        return (((double) dataSize) / 1048576).ToString("F2") + " MB";
      }
      else if (dataSize >= 1073741824)
      {
        return (((double) dataSize) / 1073741824).ToString("F2") + " GB";
      }
      return null;
    }
}

Related Tutorials