美文网首页
C# 计算文件大小(B/KB/MB/GB)

C# 计算文件大小(B/KB/MB/GB)

作者: Rinaloving | 来源:发表于2021-05-06 11:30 被阅读0次
    递归法
            public  long GetLength(double data, int i, out int index)
            {
                index = i;
                long temp = (long )(data / 1024);
                while (temp > 1024)
                {
                    i++;
                    temp = GetLength(temp, i, out index);
                }
                return temp;
    
            }
    
            public string ConvertDiskSpace(long  res, int index)
            {
                string diskSize = "";
                switch (index)
                {
    
                    case 0:
                        diskSize = $"{res.ToString("0.00")}B";
                        break;
                    case 1:
                        diskSize = $"{res.ToString("0.00")}KB";
                        break;
                    case 2:
                        diskSize = $"{res.ToString("0.00")}MB";
                        break;
                    case 3:
                        diskSize = $"{res.ToString("0.00")}GB";
                        break;
                    case 4:
                        diskSize = $"{res.ToString("0.00")}TB";
                        break;
                    default:
                        break;
                }
                return diskSize;
            }
    
    

    相关文章

      网友评论

          本文标题:C# 计算文件大小(B/KB/MB/GB)

          本文链接:https://www.haomeiwen.com/subject/rcardltx.html