views:

53

answers:

3

I want to get the file size of any given file using the C#, and I need to show it in GB, MB, KB and bytes if possible ...

and for audio (mp3) files, I need to get the duration of the file ...

+6  A: 

You can use FileInfo.Length to get the size (in bytes) of a file. Then a simple calculation can tell you KB, MB and GB:

string fileName = "C:\Path\to\file.txt";
var fileInfo = new FileInfo(fileName);

Console.WriteLine("Length = {0} bytes", fileInfo.Length);
Console.WriteLine("      or {0} KB", fileInfo.Length / 1024);
Console.WriteLine("      or {0} MB", fileInfo.Length / 1024 / 1024);
Console.WriteLine("      or {0} GB", fileInfo.Length / 1024 / 1024 / 1024);

To get the duration of an mp3 file, you'll need to use a library (such as TagLib#) that supports reading the headers of the mp3 file to parse out the duration.

Dean Harding
Divide by 1024.0 to avoid losing precision.
Grozz
A: 

Determining the file size:

FileInfo fileInfo = new FileInfo(fileName);
Console.WriteLine("Length = " + fileInfo.Length.toString());

You will have to convert that value as you wish / need.

thelost
A: 

try this:

public class FileSizeFormatProvider : IFormatProvider, ICustomFormatter
    {
        public object GetFormat(Type formatType)
        {
            if (formatType == typeof(ICustomFormatter)) return this;
            return null;
        }

        private const string fileSizeFormat = "fs";
        private const Decimal OneKiloByte = 1024M;
        private const Decimal OneMegaByte = OneKiloByte * 1024M;
        private const Decimal OneGigaByte = OneMegaByte * 1024M;

        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            if (format == null || !format.StartsWith(fileSizeFormat))
            {
                return defaultFormat(format, arg, formatProvider);
            }

            if (arg is string)
            {
                return defaultFormat(format, arg, formatProvider);
            }

            Decimal size;

            try
            {
                size = Convert.ToDecimal(arg);
            }
            catch (InvalidCastException)
            {
                return defaultFormat(format, arg, formatProvider);
            }

            string suffix;
            if (size > OneGigaByte)
            {
                size /= OneGigaByte;
                suffix = "GB";
            }
            else if (size > OneMegaByte)
            {
                size /= OneMegaByte;
                suffix = "MB";
            }
            else if (size > OneKiloByte)
            {
                size /= OneKiloByte;
                suffix = "kB";
            }
            else
            {
                suffix = " B";
            }

            string precision = format.Substring(2);
            if (String.IsNullOrEmpty(precision)) precision = "2";
            return String.Format("{0:N" + precision + "}{1}", size, suffix);

        }

        private static string defaultFormat(string format, object arg, IFormatProvider formatProvider)
        {
            IFormattable formattableArg = arg as IFormattable;
            if (formattableArg != null)
            {
                return formattableArg.ToString(format, formatProvider);
            }
            return arg.ToString();
        }

    }

In main class call it like this:

public static void Main()
        {
            FileInfo fInfo = new FileInfo(@"D:\Songs\housefull01(www.songs.pk).mp3");
  Console.WriteLine(String.Format(new FileSizeFormatProvider(), "File size: {0:fs}", fInfo.Length));
        }
anishmarokey