龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

C#快速格式化文件大小方法显示

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
分享一个格式化文件大小的 C# 方法: public static String FormatFileSize(Int64 fileSize) { if (fileSize 0) { throw new ArgumentOutOfRangeException("fileSize"); } else if (fileSize = 1024 * 1024 * 1024) { return string.Format("{

分享一个格式化文件大小的 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);
    }
}
精彩图集

赞助商链接