Return to Snippet

Revision: 39968
at January 24, 2011 07:02 by kyrathaba


Initial Code
namespace Kyrathasoft.Conversions.GetFilesizeDescription {

    using System;

    public static class clsGetFilesizeDescription {

        public static string getFilesizeDesc(double sizeInBytes) {

            const double Terabyte = 1099511627776;
            const double Gigabyte = 1073741824;
            const double Megabyte = 1048576;
            const double Kilobyte = 1024;

            string result = string.Empty;
            double the_size = 0;
            string units = string.Empty;

            if (sizeInBytes >= Terabyte) {
                the_size = sizeInBytes / Terabyte;
                units = " Tb";
            }
            else {
                if (sizeInBytes >= Gigabyte) {
                    the_size = sizeInBytes / Gigabyte;
                    units = " Gb";
                }
                else {
                    if (sizeInBytes >= Megabyte) {
                        the_size = sizeInBytes / Megabyte;
                        units = " Mb";
                    }
                    else {
                        if (sizeInBytes >= Kilobyte) {
                            the_size = sizeInBytes / Kilobyte;
                            units = " Kb";
                        }
                        else {
                            the_size = sizeInBytes;
                            units = " bytes";
                        }
                    }
                }
            }

            if (units != "bytes") {
                result = the_size.ToString("N3") + " " + units;
            }
            else {
                result = the_size.ToString() + " " + units;
            }
            return result;
        }

    }
}

Initial URL

                                

Initial Description

                                

Initial Title
converting bytes to an appropriate file size description

Initial Tags

                                

Initial Language
C#