How do I get the Hard Drive Controller Type in C#?

Jonathan Siegel 21 Reputation points
2022-07-08T19:38:16.813+00:00

I am trying to get the equivalent of the DISKPART Location Path using C#, but I can't find a way to get the hard drive controller type.

For example:

DISKPART> detail disk
...
Location Path : PCIROOT(0)#PCI(1D04)#PCI(0000)#NVME(P00T00L00)

I tried using the WMI Win32_PnPEntity::GetDeviceProperties method to get the DEVPKEY_Device_LocationPaths property, but that property isn't implemented for my hard drives.

I also tried building it by walking the PnP Device Tree using DEVPKEY_Device_Parent and capturing DEVPKEY_Device_LocationInfo. This works except the Enumerator/Interface always shows SCSI, not NVME.

Is there another API for getting the Location Path information that has the type of hard drive controller specified? I've seen Windows Storage Management API, but that just looks like WMI.

I really don't want to go the route of DISKPART scripting and parsing text. That sounds really fragile.

Thanks,
Jonathan

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
Windows Server Management
Windows Server Management
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.Management: The act or process of organizing, handling, directing or controlling something.
428 questions
{count} votes

Accepted answer
  1. Castorix31 83,206 Reputation points
    2022-07-08T22:37:20.927+00:00

    One of the ways is with MSFT_Disk and BusType
    (for example, I get 11 (SATA) for my disk)

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Michael Taylor 51,346 Reputation points
    2022-07-08T22:04:51.357+00:00

    That path you're seeing in diskpart is a combination of the path of the controller managing the drive + the drive location itself. You can get this information using WMI I believe but you'll have to do some navigating. There might be an easier way but I'd do it using WMI.

    The controller's location is everything up to the last part. In your case you have a controller with a location of PCIROOT(0)#PCI(1D04)#PCI(0000). The last part is the disk drive's location (P00T00L00`). I haven't looked into it but I suspect this lines up with bus # 0, target Id 0 and Lun 0. You can use device manager to verify.

    1 person found this answer helpful.