Win32_DiskDrive > Disk Size in GB instead of bytes

~OSD~ 2,151 Reputation points
2023-03-30T03:02:40.02+00:00

Hi,

I am working with a VB Forms application and collecting disk information. The disk size is retrieved in bytes but I want to get/convert into GB, IF POSSIBLE:

Dim DiskInfo As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_DiskDrive")
For Each queryDisk As ManagementObject In DiskInfo.Get()
RichTextBox1.AppendText("  Size (Bytes): " & queryDisk("Size") & "      " & vbCrLf)
Next
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,888 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,668 questions
0 comments No comments
{count} votes

Accepted answer
  1. TP 83,971 Reputation points
    2023-03-30T04:59:24.9466667+00:00

    Hi,

    Divide the number by bytes by 1073741824 and that will give you Gigabytes.

    If the above answers your question, please click Accept Answer.

    Thanks.

    -TP


1 additional answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 29,261 Reputation points Microsoft Vendor
    2023-03-30T08:15:01.7+00:00

    Hi @~OSD~,

    Don't know if queryDisk("Size") gets a plain numeric string, if so, you can try the following code, if not, you may have to extract its numeric part first.

    RichTextBox1.AppendText("  Size (Gigabytes): " & queryDisk("Size") / 1073741824 & "      " & vbCrLf)
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.