我正在试验我在以下链接中从 MSDN 中找到的一些代码。
https://learn.microsoft.com/en-us/dotnet/api/system.io.driveinfo.driveformat?view=net-7.0
我想知道如何让程序将结果列为 GB 或 TB 而不是字节? 虽然我知道如何将字节转换为成功地将结果列为 GB ( TotalFreeSpace / 2 ^ 30 ),但我在成功将结果列为 TB 的程序中遇到了问题。
我明白 ( TotalFreeSpace / 2 ^ 40 ) SHOULD 成功地将字节转换为 TB 列表结果;但是,我不知道如何让控制台应用程序将结果列为 GB 和 TB 而不是字节。
标准代码如下
Class Test
Public Shared Sub Main()
Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
Dim d As DriveInfo
For Each d In allDrives
Console.WriteLine("Drive {0}", d.Name)
Console.WriteLine(" Drive type: {0}", d.DriveType)
If d.IsReady = True Then
Console.WriteLine(" Volume label: {0}", d.VolumeLabel)
Console.WriteLine(" File system: {0}", d.DriveFormat)
Console.WriteLine(
" Total available space: {0, 15} bytes",
d.TotalFreeSpace)
Console.WriteLine(
" Total size of drive: {0, 15} bytes ",
d.TotalSize)
End If
Next
End Sub
End Class
虽然我知道实施以下更改将成功将结果列为 GB。
Class Test
Public Shared Sub Main()
Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
Dim d As DriveInfo
For Each d In allDrives
Console.WriteLine("Drive {0}", d.Name)
Console.WriteLine(" Drive type: {0}", d.DriveType)
If d.IsReady = True Then
Console.WriteLine(" Volume label: {0}", d.VolumeLabel)
Console.WriteLine(" File system: {0}", d.DriveFormat)
Console.WriteLine(
" Total available space: {0, 15} bytes",
d.TotalFreeSpace / 2 ^ 30)
Console.WriteLine(
" Total size of drive: {0, 15} bytes ",
d.TotalSize / 2 ^ 30)
End If
Next
End Sub
End Class
并且实施以下更改应该成功地将结果列为 TB。
Class Test
Public Shared Sub Main()
Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
Dim d As DriveInfo
For Each d In allDrives
Console.WriteLine("Drive {0}", d.Name)
Console.WriteLine(" Drive type: {0}", d.DriveType)
If d.IsReady = True Then
Console.WriteLine(" Volume label: {0}", d.VolumeLabel)
Console.WriteLine(" File system: {0}", d.DriveFormat)
Console.WriteLine(
" Total available space: {0, 15} bytes",
d.TotalFreeSpace / 2 ^ 40)
Console.WriteLine(
" Total size of drive: {0, 15} bytes ",
d.TotalSize / 2 ^ 40)
End If
Next
End Sub
End Class
如何让程序以 GB 或 TB 而非字节为单位列出结果?我有四个驱动器,两个以 GB 为单位,两个以 TB 为单位。如何在控制台应用程序中正确列出这些驱动器?另外,如果可能的话,我怎样才能删除小数位以仅在结果后包含两位小数(EX 2.14 TB。)