在 C# 中格式化 DriveInfo 的 TotalSize 结果
所以我们都知道下面的代码将返回一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为是 114 GB,但是嘿。无论如何,我会为此编写一个辅助函数。就像...
编辑:正如所指出的,我完全忘记了处理大小(代码已修改)
I think that is 114 GB but hey. Anyway, I would write a helper function for this. Something like...
EDIT: As pointed out, I had complete forgot to deal with the size (code amended)
这是我正在使用的片段:
This is the snippet i'm using:
是的。重复除以 1024。
Yes. Repeated division by 1024.
我只是想补充一点,如果您谈论的是驱动器的大小而不是其他东西的大小,请注意 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.