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

Auto generate change scripts in SQL Server Management Studio SSMS for tables

How to debug Windows services in Visual Studio?

How to create RSS Feed in Asp.Net