I want to get infos like PID, VID and SerialNumber from a Pendrive in C#, dotnet 6.
Im gonna use this to generate a dongle key.
I want to search this infos from a specific (letter, name etc.) pendrive, can't be the first or anything else, because maybe the user will have more than once pendrive plugged.
I've tried to use ManagementObjectSearcher.
ManagementObjectSearcher searcher = new ManagementObjectSearcher($"SELECT * FROM Win32_PnPEntity WHERE (PNPClass = 'USB')
But I can't search by the letter, or by the name of the pendrive.
So I need a way to list the pendrives and choose the pendrive.
Right, so the annoying thing is that all the info you want actually come from 3 separate locations .
Win32_PnPEntityWin32_DiskDriveD:) comes fromWin32_LogicalDiskThese 3 queries are likely to give you the same number of results, representing the same drives, but giving you different pieces of the information:
They are related, but their links are not the easiest to tease out. In this example, we will start from the Drive Letters, traversing the connections using ASSOCIATORS OF, through this path:
LogicalDisk -> DiskPartition -> DiskDrive -> PnPEntityWhich, on my system with one USB stick conected, returns:
Of course, you have the possibility of using an un-formatted drive. In which case, you will neither have a
Partitionnor aLogicalDrive. In which case, you may want to start fromDiskDrive, and traverse the associations from there.