Loveyuki's BLOG

L-BLOG.NET, The Matrix Reloaded.

存档:2008-01-05
C# 中快速格式化文件大小显示
Loveyuki | 学习笔记 | 2008-01-05 16:05 | 1560 点击 | 7 评论 | C#

分享一个格式化文件大小的 C# 方法:

public static String FormatFileSize(Int64 fileSize)
{
    if (fileSize < 0)
    {
        throw new ArgumentOutOfRangeException("fileSize");
    }
    else if (fileSize >= 1024 * 1024 * 1024)
    {
        return string.Format("{0:########0.00} GB", ((Double)fileSize) / (1024 * 1024 * 1024));
    }
    else if (fileSize >= 1024 * 1024)
    {
        return string.Format("{0:####0.00} MB", ((Double)fileSize) / (1024 * 1024));
    }
    else if (fileSize >= 1024)
    {
        return string.Format("{0:####0.00} KB", ((Double)fileSize) / 1024);
    }
    else
    {
        return string.Format("{0} bytes", fileSize);
    }
}
 

版权所有©2007-2008, Loveyuki.com | 系统:Bitrac | 皮肤:Blog Pixel | 空间域名:光辉互联 易联网络