惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
The Blog of Author Tim Ferriss
S
Securelist
D
Docker
The Register - Security
The Register - Security
GbyAI
GbyAI
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
罗磊的独立博客
博客园 - 【当耐特】
F
Full Disclosure
WordPress大学
WordPress大学
腾讯CDC
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
I
InfoQ
MyScale Blog
MyScale Blog
量子位
Cyberwarzone
Cyberwarzone
博客园 - 三生石上(FineUI控件)
The Hacker News
The Hacker News
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
博客园_首页
H
Help Net Security
K
Kaspersky official blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
The Cloudflare Blog
P
Proofpoint News Feed
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
爱范儿
爱范儿
P
Privacy International News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
B
Blog RSS Feed

博客园 - ю意思я

视频播放的进化(一) 图片同步 WPF初体验 C#实现开关显示器功能 图片裁剪 共享文件夹 w32Time服务(NTP)的一些配置 ShutdownAPI 将文件读取到内存中 在windows server 2003服务器上提供NTP时间同步服务 如何与使用DHCP获取IP地址的设备组网 在VS内添加Web Reference 如何消除锯齿,在图片上画出高质量的文字 如何让Firefox3支持迅雷 获取和设置IP地址 判断字符串是否为IPv4地址 axWindowsMediaPlayer操作 关于相对路径匹配的问题 Class的设计
ToFriendlyFileSize
ю意思я · 2008-06-11 · via 博客园 - ю意思я

  通过FileInfo类获得的文件大小是long类型的,并非我们习惯的格式,因此为long类型添加一个扩展方法:

        public static string ToFriendlyFileSize(this double arg) {
            
string result = "N/A";
            
double size = arg;

            
const double p1 = 1024;
            
const double p2 = 1048576;                // Math.Pow(1024,2)
            const double p3 = 1073741824;            // Math.Pow(1024,3)
            const double p4 = 1099511627776;        // Math.Pow(1024,4)
            const double t1 = 1000;
            
const double t2 = 1000000;                // Math.Pow(1000,2)
            const double t3 = 1000000000;            // Math.Pow(1000,3)
            const double t4 = 1000000000000;        // Math.Pow(1000,4)
            const double t5 = 1000000000000000;    // Math.Pow(1000,5)

            
if (size < 0 || size >= t5)
                
throw new ArgumentOutOfRangeException();

            
if (size >= 0 && size < t1) {
                result 
= string.Format("{0:G3} B", size);
            }
 else if (size >= t1 && size < p1) {
                result 
= string.Format("{0:G2} KB", size / p1);
            }
 else if (size >= p1 && size < t2) {
                result 
= string.Format("{0:G3} KB", size / p1);
            }
 else if (size >= t2 && size < p2) {
                result 
= string.Format("{0:G2} MB", size / p2);
            }
 else if (size >= p2 && size < t3) {
                result 
= string.Format("{0:G3} MB", size / p2);
            }
 else if (size >= t3 && size < p3) {
                result 
= string.Format("{0:G2} GB", size / p3);
            }
 else if (size >= p3 && size < t4) {
                result 
= string.Format("{0:G3} GB", size / p3);
            }
 else if (size >= t4 && size < p4) {
                result 
= string.Format("{0:G2} TB", size / p4);
            }
 else if (size >= p4 && size < t5) {
                result 
= string.Format("{0:G3} TB", size / p4);
            }


            
return result;
        }


        
public static string ToFriendlyFileSize(this long arg) {
            
double size = arg;
            
return size.ToFriendlyFileSize();
        }

【意思原创,转载请注明出处,谢谢】