Detect if System (Windows) drive is SSD or HDD?

OSVBNET 1,386 Reputation points
2021-04-08T22:56:16.693+00:00

Hey,
I have found this code:

Dim MyScope = New ManagementScope("\\.\root\microsoft\windows\storage")
Dim MySearcher = New ManagementObjectSearcher("SELECT * FROM MSFT_PhysicalDisk")
Dim Type As String = ""
MyScope.Connect()
MySearcher.Scope = MyScope
Dim rootDrive As String = Path.GetPathRoot(Environment.SystemDirectory)
Dim queryObj As ManagementObject
....

Now, I have HDD, SSD and external drive attached to the system, I just wanna to get the type of my System drive if it's SSD or HDD?
But:
MySearcher.Get(0).queryObj("MediaType")
Won't work, how to make it work?
Thanks :)

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,578 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Castorix31 81,741 Reputation points
    2021-04-09T04:46:08.137+00:00

    What do you mean by "Won't work" ?
    I cannot do a real test because I don't have any SSD, but for example, on my OS, if I test :

    Try
        Dim searcher As New ManagementObjectSearcher("root\Microsoft\Windows\Storage", "SELECT * FROM MSFT_PhysicalDisk")
        For Each queryObj As ManagementObject In searcher.Get()
            Console.WriteLine("DeviceId: {0}", queryObj("DeviceId"))
            Console.WriteLine("Model: {0}", queryObj("Model"))
            Dim sMediaType As String = Nothing
            Select Case queryObj("MediaType")
                Case 0
                    sMediaType = "Unspecified"
                Case 3
                    sMediaType = "HDD"
                Case 4
                    sMediaType = "SSD"
                Case 5
                    sMediaType = "SCM"
                Case Else
                    sMediaType = "Not recognized"
            End Select
            Console.WriteLine("MediaType: {0}", sMediaType)
        Next
    Catch ex As ManagementException
        MessageBox.Show("An error occurred while querying for WMI data: " & ex.Message)
    End Try
    

    I get :

            'DeviceId: 0
            'Model:      ST1000DM003-1SB1
            'MediaType:  HDD
    
    0 comments No comments

  2. OSVBNET 1,386 Reputation points
    2021-04-09T07:44:22.71+00:00

    Thanks dude, I mean if I don't wanna use For Each and just passing 0 index:

    MySearcher.Get(0).queryObj("MediaType")

    This won't work after my above code! :(


  3. OSVBNET 1,386 Reputation points
    2021-04-09T12:23:21.053+00:00

    Thank you very very much dude,

    There's one more problem, in my original code above, I have:

    Dim rootDrive As String = Path.GetPathRoot(Environment.SystemDirectory)

    Because I don't wanna get all physical drives, and just the system drive, how can I use the above live instead of "SELECT * FROM MSFT_PhysicalDisk" ?

    Thanks indeed :)

    0 comments No comments

  4. Castorix31 81,741 Reputation points
    2021-04-09T12:47:38.373+00:00

    [ Cannot answer in comments (> 1000 chars...)]

    There's one more problem, in my original code above, I have:

    Dim rootDrive As String = Path.GetPathRoot(Environment.SystemDirectory)

    Because I don't wanna get all physical drives, and just the system drive, how can I use the above live instead of "SELECT * FROM MSFT_PhysicalDisk" ?

    For example :

    Dim rootDrive As String = Path.GetPathRoot(Environment.SystemDirectory)
    rootDrive = rootDrive.Substring(0, rootDrive.Length - 1)
    Dim sDriveIndex As String = GetPhysicalDriveId(rootDrive, True)
    
    Dim searcher As New ManagementObjectSearcher("root\Microsoft\Windows\Storage", "SELECT * FROM MSFT_PhysicalDisk WHERE DeviceId = " & sDriveIndex)
    ' code ...
    

    with (from Google) :

    Private Function GetPhysicalDriveId(drvName As String, Optional ReturnIndex As Boolean = False) As String
        Dim devId As String = ""
        Using LogicalDiskQueryResults As New ManagementObjectSearcher("ASSOCIATORS OF {Win32_LogicalDisk.DeviceID='" & (drvName & "'} WHERE AssocClass = Win32_LogicalDiskToPartition"))
            For Each mo As ManagementObject In LogicalDiskQueryResults.Get
                Using DiskPartitionQueryResults As New ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & (mo("DeviceID").ToString & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"))
                    For Each partition As ManagementObject In DiskPartitionQueryResults.Get
                        Dim propName As String = If(ReturnIndex, "Index", "DeviceID")
                        devId = partition(propName).ToString 
                    Next
                End Using
            Next
        End Using
        Return devId
    End Function