How do I get a file size in bytes using .NET?

public string GetFileSize(string filename)
    {

        string[] sizes = { "B", "KB", "MB", "GB" };
        double len = new FileInfo(filename).Length;
        int order = 0;
        while (len >= 1024 && order + 1 < sizes.Length)
        {
            order++;
            len = len / 1024;
        }
      return String.Format("{0:0.##} {1}", len, sizes[order]);
    }

Comments

Popular posts from this blog

Executing PowerShell scripts from C#

HOW TO Use the NumericUpDown Control

Exposing Agile Software Development Myths