How do I get physical drive number like \\.\PhysicalDrive0 from a drive letter

galaksikomputer

New member
Joined
Jan 5, 2024
Messages
3
Programming Experience
3-5
Hi, I want to list physical drive information, just by inputting the drive letter

if i set input "C:\" than the code will display like \\.\PhysicalDrive0
if i set input "D:\ than the output will be \\.\PhysicalDrive1

based on connected drive to computer, try to search on google and forums not found any solution yet
can anyone help me?
 
You would likely have to use WMI. I would expect classes like CIM_LogicalDisk, CIM_DiskPartition and CIM_DiskDrive to be involved. I don't have the time to go into specifics right now but here are some useful links. I suggest that you try to learn and understand what's going on rather than just copy/pasting code.


That last one is only relevant if you're targeting .NET Core, which includes .NET 5 and later.
 
I have read those and have few sample, but not like i need, most is getting the partition information , disk information and get the drive letter using WMI, I need physical drive location on the computer, based on the drive letter , I use VB.net with visual studio 2022 , thanks for the help, appreciated
 
I found a powershell script that I just converted to vb.net

Get drives:
        Dim objWMIService = GetObject("winmgmts:\\.\root\cimv2")
        Dim colDiskDrives = objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive")
        Dim strDeviceID As String
        Dim colPartitions, colLogicalDisks
        Dim WMIQuery As String

        For Each objDrive In colDiskDrives
            lbDrives.Items.Add("Physical Disk: " & objDrive.Caption & " — " & objDrive.DeviceID)

            strDeviceID = Replace(objDrive.DeviceID, "\", "\\")
            WMIQuery = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strDeviceID & """} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
            colPartitions = objWMIService.ExecQuery(WMIQuery)

            For Each objPartition In colPartitions
                lbDrives.Items.Add("Disk Partition: " & objPartition.DeviceID)

                WMIQuery = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & objPartition.DeviceID & """} WHERE AssocClass = Win32_LogicalDiskToPartition"
                colLogicalDisks = objWMIService.ExecQuery(WMIQuery)

                For Each objLogicalDisk In colLogicalDisks
                    lbDrives.Items.Add("Logical Disk: " & objLogicalDisk.DeviceID)
                Next
            Next

        Next
You can convert this to a function that you can pass the drive letter to and have it return the physical drive it is associated with

Found it here: How Can I Correlate Logical Drives and Physical Disks? - Scripting Blog [archived]
 

Attachments

  • Screenshot 2024-01-06 135056.png
    Screenshot 2024-01-06 135056.png
    20.1 KB · Views: 5
I found a powershell script that I just converted to vb.net

Get drives:
        Dim objWMIService = GetObject("winmgmts:\\.\root\cimv2")
        Dim colDiskDrives = objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive")
        Dim strDeviceID As String
        Dim colPartitions, colLogicalDisks
        Dim WMIQuery As String

        For Each objDrive In colDiskDrives
            lbDrives.Items.Add("Physical Disk: " & objDrive.Caption & " — " & objDrive.DeviceID)

            strDeviceID = Replace(objDrive.DeviceID, "\", "\\")
            WMIQuery = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strDeviceID & """} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
            colPartitions = objWMIService.ExecQuery(WMIQuery)

            For Each objPartition In colPartitions
                lbDrives.Items.Add("Disk Partition: " & objPartition.DeviceID)

                WMIQuery = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & objPartition.DeviceID & """} WHERE AssocClass = Win32_LogicalDiskToPartition"
                colLogicalDisks = objWMIService.ExecQuery(WMIQuery)

                For Each objLogicalDisk In colLogicalDisks
                    lbDrives.Items.Add("Logical Disk: " & objLogicalDisk.DeviceID)
                Next
            Next

        Next
You can convert this to a function that you can pass the drive letter to and have it return the physical drive it is associated with

Found it here: How Can I Correlate Logical Drives and Physical Disks? - Scripting Blog [archived]

thank you it helps alot.. (y);)
 
Back
Top