How to get program to list results in GB or TB rather than bytes?

130 Views Asked by At

I was experimenting with some code that I found from the MSDN at the following link.

https://learn.microsoft.com/en-us/dotnet/api/system.io.driveinfo.driveformat?view=net-7.0

I am wondering how I can have the program list the results as GB or TB rather than in bytes? While I know how to convert bytes to list results as GB successfully ( TotalFreeSpace / 2 ^ 30 ) I am having issues with the program successfully listing results as TB.

I understand that ( TotalFreeSpace / 2 ^ 40 ) SHOULD convert bytes to list results as TB successfully; however, I am unaware as to how to have the console application list both results as GB and TB rather than bytes.

The standard code is as follows.

 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

While I understand that implementing the following changes will list results as GB successfully.

 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

And Implementing the following changes should list results as TB successfully.

 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

How can I have the program list the results in GB or TB rather than in bytes? I have four drives and two are sized in GB and two are sized in TB. How can I have those drives listed properly within the console application? Also if possible how can I remove the decimal places to only contain two decimal places after the results (EX 2.14 TB.)?

1

There are 1 best solutions below

0
dpresnell90 On

I just wanted to say thank you to everyone in helping me out with this. After using @jmcilhinney's answer above and some conditional formatting I was able to get the program to list the results properly.

Below is the code I utilized to achieve the results I sought after, in the hope that it may help someone out in the future.

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 And d.TotalSize < 1099511627776 Then
                Console.WriteLine("  Volume label: {0}", d.VolumeLabel)
                Console.WriteLine("  File system: {0}", d.DriveFormat)


                Console.WriteLine(
            "  Total size of drive:   {0, 15} GB ",
            FormatNumber(d.TotalSize / 1024 / 1024 / 1024))


            ElseIf d.IsReady = True And d.TotalSize >= 1099511627776 Then
                Console.WriteLine("  Volume label: {0}", d.VolumeLabel)
                Console.WriteLine("  File system: {0}", d.DriveFormat)



                Console.WriteLine(
            "  Total size of drive:   {0, 15} TB ",
            FormatNumber(d.TotalSize / 1024 / 1024 / 1024 / 1024))
            End If

            If d.IsReady = True And d.TotalSize - d.TotalFreeSpace < 1099511627776 Then



                Console.WriteLine(
                "  Total used space:      {0, 15} GB",
                FormatNumber(d.TotalSize / 1024 / 1024 / 1024 - d.TotalFreeSpace / 1024 / 1024 / 1024))


            ElseIf d.IsReady = True And d.TotalSize - d.TotalFreeSpace >= 1099511627776 Then


                Console.WriteLine(
                "  Total used space:      {0, 15} TB",
                FormatNumber(d.TotalSize / 1024 / 1024 / 1024 / 1024 - d.TotalFreeSpace / 1024 / 1024 / 1024 / 1024))
            End If


            If d.IsReady = True And d.TotalFreeSpace < 1099511627776 Then


                Console.WriteLine(
                "  Total available space: {0, 15} GB",
                FormatNumber(d.TotalFreeSpace / 1024 / 1024 / 1024))

            ElseIf d.IsReady = True And d.TotalFreeSpace >= 1099511627776 Then



                Console.WriteLine(
            "  Total available space: {0, 15} TB",
            FormatNumber(d.TotalFreeSpace / 1024 / 1024 / 1024 / 1024))
            End If
        Next
    End Sub
End Class

Please note the the conversion is needed as the original code snippet that this is based off of is meant to retrieve drive information in bytes.

Original code can be found in the MSDN located in the hyperlink below:

https://learn.microsoft.com/en-us/dotnet/api/system.io.driveinfo.driveformat?view=net-7.0

Thank everyone for their time and help regarding this question. Hopefully this will benefit someone in the future.

Please note this was utilized in a Visual Basic Console Application.

Results are listed below:

Console Application Results