在 C# 中格式化 DriveInfo 的 TotalSize 结果

发布于 2024-12-01 21:54:29 字数 353 浏览 0 评论 0原文

所以我们都知道下面的代码将返回一个 long:

DriveInfo myDrive = new DriveInfo("C:\\");
long size = myDrive.TotalSize;
Console.WriteLine("Drive Size is: {0}", size);

输出将是这样的:

Drive Size is: 114203439104

所以我认为这意味着驱动器的总大小约为 114 GB。

但是,我想将其转换为以下格式:

114.2 MB

有没有一种真正快速且简单的方法来做到这一点?

提前致谢。

So we all know that the following code will return a long:

DriveInfo myDrive = new DriveInfo("C:\\");
long size = myDrive.TotalSize;
Console.WriteLine("Drive Size is: {0}", size);

The output will be something like this:

Drive Size is: 114203439104

So I think this means that the drive has a total size of around 114 GigaBytes.

However, I want to get this into the following format:

114.2 MB

Is there a really quick and easy way of doing this?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

揪着可爱 2024-12-08 21:54:29

我认为是 114 GB,但是嘿。无论如何,我会为此编写一个辅助函数。就像...

public string GetSize(long size)
{
   string postfix = "Bytes";
   long result = size;
   if(size >= 1073741824)//more than 1 GB
   {
      result = size / 1073741824;
      postfix = "GB";
   }
   else if(size >= 1048576)//more that 1 MB
   {
      result = size / 1048576;
      postfix = "MB";
   }
   else if(size >= 1024)//more that 1 KB
   {
      result = size / 1024;
      postfix = "KB";
   }

   return result.ToString("F1") + " " + postfix;
}

编辑:正如所指出的,我完全忘记了处理大小(代码已修改)

I think that is 114 GB but hey. Anyway, I would write a helper function for this. Something like...

public string GetSize(long size)
{
   string postfix = "Bytes";
   long result = size;
   if(size >= 1073741824)//more than 1 GB
   {
      result = size / 1073741824;
      postfix = "GB";
   }
   else if(size >= 1048576)//more that 1 MB
   {
      result = size / 1048576;
      postfix = "MB";
   }
   else if(size >= 1024)//more that 1 KB
   {
      result = size / 1024;
      postfix = "KB";
   }

   return result.ToString("F1") + " " + postfix;
}

EDIT: As pointed out, I had complete forgot to deal with the size (code amended)

巾帼英雄 2024-12-08 21:54:29

这是我正在使用的片段:

    public static string FormatBytesToHumanReadable(long bytes)
    {
        if (bytes > 1073741824)
            return Math.Ceiling(bytes / 1073741824M).ToString("#,### GB");
        else if (bytes > 1048576)
            return Math.Ceiling(bytes / 1048576M).ToString("#,### MB");
        else if (bytes >= 1) 
            return Math.Ceiling(bytes / 1024M).ToString("#,### KB");
        else if (bytes < 0)
            return "";
        else
            return bytes.ToString("#,### B");
    }

This is the snippet i'm using:

    public static string FormatBytesToHumanReadable(long bytes)
    {
        if (bytes > 1073741824)
            return Math.Ceiling(bytes / 1073741824M).ToString("#,### GB");
        else if (bytes > 1048576)
            return Math.Ceiling(bytes / 1048576M).ToString("#,### MB");
        else if (bytes >= 1) 
            return Math.Ceiling(bytes / 1024M).ToString("#,### KB");
        else if (bytes < 0)
            return "";
        else
            return bytes.ToString("#,### B");
    }
星光不落少年眉 2024-12-08 21:54:29

是的。重复除以 1024。

var kb = size/1024;
var mb = kb/1024;

Yes. Repeated division by 1024.

var kb = size/1024;
var mb = kb/1024;
ぇ气 2024-12-08 21:54:29

我只是想补充一点,如果您谈论的是驱动器的大小而不是其他东西的大小,请注意 HDD/SDD 硬件供应商使用 1000 KB,而不是 1024。这就是为什么标记为 400Gb 的 HDD 将显示为大多数程序为 372.53GB。确保向用户提供他期望的信息。

I just want to add, that if you're talking about size of drive and not about size of something else, be aware that HDD/SDD hardware vendors use 1000 for KB, not 1024. That's why HDD marked as 400Gb will be shown as 372.53GB in most programs. Be sure you provide your user with information he expects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文