question

osvbnet avatar image
0 Votes"
osvbnet asked osvbnet action

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

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 :)

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

osvbnet avatar image
0 Votes"
osvbnet answered Castorix31 commented

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! :(

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

From my sample, without loop :

     Dim queryObj1 As ManagementObject = searcher.Get()(0)
     Dim nMediaType = queryObj1("MediaType")
0 Votes 0 ·
osvbnet avatar image
0 Votes"
osvbnet answered

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 :)

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered Castorix31 commented

[ 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
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hey again, after the above solution, when I wanna use:

Dim MyQuery As ManagementObject = MySearcher.Get(0)

Now I don't need to pass index, so how to get the selected system drive? Thanks...

0 Votes 0 ·

Like in the other comment, you get the first index (0) :

             Dim MyQuery As ManagementObject = MySearcher.Get()(0)
             Dim nMediaType = MyQuery("MediaType")
             // ' code with Select Case
0 Votes 0 ·