Format File Size : File Command « File Stream « C# / C Sharp






Format File Size

     

using System;
using System.Collections.Generic;
using System.Text;

namespace s3
{
    static class Utils
    {
        private static readonly double kilobyte = 1024;
        private static readonly double megabyte = 1024 * kilobyte;
        private static readonly double gigabyte = 1024 * megabyte;
        private static readonly double terabyte = 1024 * gigabyte;

        public static string FormatFileSize(double bytes)
        {
            if (bytes > terabyte) return (bytes / terabyte).ToString("0.00 TB");
            else if (bytes > gigabyte) return (bytes / gigabyte).ToString("0.00 GB");
            else if (bytes > megabyte) return (bytes / megabyte).ToString("0.00 MB");
            else if (bytes > kilobyte) return (bytes / kilobyte).ToString("0.00 KB");
            else return bytes + " bytes";
        }


    }
}

   
    
    
    
    
  








Related examples in the same category

1.C# Implementation of the File Copy Command
2.Use the FileInfo Class to Delete Files with Ease
3.File Move Implementation
4.Rename File Name
5.Rename File Extension
6.Backup File
7.Copy directory
8.Find string in a file
9.Converts a number of bytes to a shorter, more readable string representation
10.Compares the specified file1. This method accepts two strings the represent two files to compare.
11.File Renamer
12.Is File Existed
13.Is File readable or writeable
14.Replace in file
15.Send a file to the recycle bin
16.Copy file and folder
17.Gets last write time for a file.
18.Execute File
19.Gets the file size text.
20.Get File Bytes And Readable Size Information
21.Copy folder
22.Empty a Folder