Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine if System drive is HDD or SSD 1

Status
Not open for further replies.

ComputersUnlimited

IS-IT--Management
Dec 18, 2013
37
US
I am writing a program in VB 2019 where I need to know whether the system drive is a HDD or SSD. Is this possible and if so how?

Thank You
 
Sure. This illustrates how to query all you physical drives and determine the media type. SHould be able to use this as the basis for yourb requirement:

Code:
[blue]Imports System.IO
Imports System.Management
Module Module2
    Public Sub EnumPhysicalDriveTypes()
        Dim scope = New ManagementScope("\\.\root\microsoft\windows\storage")
        Dim searcher = New ManagementObjectSearcher("SELECT * FROM MSFT_PhysicalDisk")
        Dim Type As String = ""
        scope.Connect()
        searcher.Scope = scope

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

        For Each queryObj In searcher.Get()
            Select Case queryObj("MediaType")
                Case 1
                    Type = "Unspecified"
                Case 3
                    Type = "HDD"
                Case 4
                    Type = "SSD"
                Case 5
                    Type = "SCM"
                Case Else
                    Type = "Unspecified"
            End Select

            MsgBox("Device " & queryObj("DeviceID") & ": " & Type)
        Next
        searcher.Dispose()
    End Sub

End Module[/blue]
 
I copied your code into a new project and have received some errors

Type 'ManagementScope' is not defined
Type 'ManagementObjectSearcher' is not defined
Type 'ManagementObject' is not defined

Namespace or type specified in the Imports 'System.Management' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases

Also will this work in a console rather then a GUI program
 
Ah, I forget not everyone will necessarily have the relevant package available..
Got to

[tt]Project > Manage NuGet Packages > Browse[/tt]

and search for and get System.Management.

Alternatively, hover over ManagementScope in the IDE (it ought to be showing as an error there), and hit [tt]Alt + Enter[/tt] to show potential fixes. Then simply select [tt]Install package 'System.Management'[/tt]

And yes it works in Console mode (but not the .NET Core console mode)

You'll also need to switch Option Strict to off ...

 
After not being able to work on this for a few days due to work I now have a few days to work on it.

After adding the necessary package, it now works. However this provides the they type for each drive attached to the system, including USB drives. I need the drive type for a specific drive, the boot drive.
 
I know. As I said, you "hould be able to use this as the basis for yourb requirement"

I assumed, since you said "I need to know whether the system drive is a HDD or SSD", that you already knew which device was your system drive. And if you don't, then you may just want to clarify what you mean by 'system drive', since Microsoft use a slightly different definiton from the standard ... (specifically they basically reverse the definition of boot partition and system partition because, well, because they are Microsoft)


 
The program I am working on will be run on multiple systems, so the system drive, or boot drive, will change.

Where can I find more information on the System Management package, Management Scope and Management Object Searcher. Nothing I have found provides the different options or properties that can be retrieved. At this point in time I think I need an If statement to make sure I have the system or boot drive but what I don't know is what to put in that if statement and nothing I have found while researching has even given me a clue, most likely because I don't know what to search for.
 
So your actual question is "How do I determine which is the boot/system drive, and how do I tell if it an SSD or not", yes?

And thus my question to you remains: do you mean the system partition or the boot partition? They are not the same thing (although they may often be on the same physical drive)

Documentation can be found here:
 
So your actual question is "How do I determine which is the boot/system drive, and how do I tell if it an SSD or not", yes?"

Correct

I will need the system partition.
 
This is how Microsoft define system and boot partition:

[ul]
[li]The system partition (or system volume) is a primary partition that contains the boot loader, a piece of software responsible for booting the operating system. This partition holds the boot sector and is marked active.[/li]
[li]The boot partition (or boot volume) is the disk partition that contains the operating system folder, known as the system root or %systemroot%[/li]
[/ul]

So, are you sure you want the system partition?
 
Everything I have found says the opposite.

Either way I need the partition that Windows is installed on.
 
As I said in my post of 10:28 yesterday, "[MS] basically reverse the definition"

OPk, so, we want what Microsoft call the Boot partition, and we want o know if that partition is an SSD.

So something like:

Code:
[blue]Module Example
    Public Enum Partitiontype
        boot
        system
    End Enum

    Public Function GetPartitionMediaType(ByVal Part As Partitiontype) As String
        Dim strScope As String = "\\.\root\microsoft\windows\storage"
        Dim PartType As String = ""
        Dim DiskID As String = ""
        Dim MediaType As String = ""

        Select Case Part
            Case Partitiontype.boot
                PartType = "IsBoot"
            Case Partitiontype.system
                PartType = "IsSystem"
        End Select

        Dim searcher = New ManagementObjectSearcher(strScope, "SELECT * FROM MSFT_Partition WHERE " & PartType & "=TRUE")

        For Each queryObj As ManagementObject In searcher.Get()
            DiskID = queryObj("DiskNumber")
        Next

        searcher = New ManagementObjectSearcher(strScope, "SELECT * FROM MSFT_PhysicalDisk WHERE DeviceID = " & DiskID)

        For Each queryObj As ManagementObject In searcher.Get()
            Select Case queryObj("MediaType")
                Case 1
                    MediaType = "Unspecified"
                Case 3
                    MediaType = "HDD"
                Case 4
                    MediaType = "SSD"
                Case 5
                    MediaType = "SCM"
                Case Else
                    MediaType = "Unspecified"
            End Select

        Next

        GetPartitionMediaType = MediaType

    End Function

End Module[/blue]

And this can be called like

Code:
[blue]        MsgBox("Boot drive is " & GetPartitionMediaType(Partitiontype.boot))
        MsgBox("System drive is " & GetPartitionMediaType(Partitiontype.system))[/blue]



 
Thank you, this code put me on the right path. However I have only tested it on one system but it appears to be what I need.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top