Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to get the partition layout/scheme of a USB disk using the drive letter. I'd like to find out if the disk is MBR or GPT. I can use Powerhell but it takes too long to get information back. I have looked at MSFT_Disk and MSFT_Partition but i don't know how to use them.

What I have tried:

Dim driveLetter As String = USB1 & ":" ' Replace with your drive letter

Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_DiskPartition where DriveLetter = '" & driveLetter & "'")
For Each queryObj As ManagementObject In searcher.Get()
    Dim partitionStyle As String = queryObj("DriveType").ToString()
    If partitionStyle = "3" Then
        PartitionType = ("GPT")
        MsgBox(partitionStyle)
    ElseIf partitionStyle = "2" Then
        PartitionType = ("MBR")
        MsgBox(partitionStyle)
    Else
        PartitionType = ("NR")
        MsgBox(partitionStyle)
    End If
Next
Posted
Comments
Dave Kreskowiak 22-Jan-24 12:23pm    
If you're going to work with WMI more than this, it helps to use a tool to help you explore the namespaces and classes. Get a copy of WMI Explorer from https://github.com/vinaypamnani/wmie2/releases

First, the Win32_DiskPartition class does not have a "DriveLetter" property, that query is going to fail every time.

Next, drive letters are Logical Disks, not physical ones. A single physical disk can contain multiple logical disks, each assigned to a Win32_DiskPartition instance through the Win32_LogicalDiskToPartition class.

Since WMI does not use a JOIN query, you have to rewrite it as an ASSOCIATORS OF query, like this:
WQL
ASSOCIATORS OF {Win32_LogicalDisk.DeviceId='E:'} WHERE ResultClass=Win32_DiskPartition
This query will get you the Win32_DiskPartition object for the specified drive letter.
 
Share this answer
 
Function GetDiskPartitionType(diskDrive As String) As String
        Dim upperCaseDiskLetter As String = diskDrive.Substring(0, 1).ToUpper()
        Dim wmiQuery As String = $"SELECT DiskNumber, GptType, MbrType FROM MSFT_Partition WHERE DriveLetter='{upperCaseDiskLetter}'"
        Dim scope As New ManagementScope("\\localhost\ROOT\Microsoft\Windows\Storage")
        Dim partitionSearcher As New ManagementObjectSearcher(scope, New ObjectQuery(wmiQuery))

        For Each partition As ManagementObject In partitionSearcher.Get()
            Dim gptType As String = partition("GptType")?.ToString()
            Dim mbrType As Integer = Convert.ToInt32(partition("MbrType"))

            If Not String.IsNullOrEmpty(gptType) Then
                Return "GPT"
            ElseIf mbrType <> 0 Then
                Return "MBR"
            Else
                Return "Unknown"
            End If
        Next

        Return "Unknown"
    End Function
 
Share this answer
 
Comments
Maciej Los 22-Jan-24 11:15am    
Is this an answer?
johnjsm 22-Jan-24 11:30am    
Yeah. I just stumbled across this and it seems to work for what I need.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900