Business scenario: when developing a device that requires USB to serial port, the com name may change every connection with the computer. Solution: the easiest way to think about it is to write the serial port number in the configuration file (or let the user select the serial port number on the UI), so the actual operation is still complicated, the following is an article from codeproject.ArticleTo automatically search for specific serial devices based on certain characteristics of the serial device.
For example, if the path of the serial port contains "fdtibus" to determine whether it is the device I want to find, print it out.CodeAs follows:
using system; using system. management; namespace consoleapplication2 {class program {static void main (string [] ARGs) {try {var searcher = new managementobjectsearcher ("Root \ WMI ", "select * From msserial_portname"); foreach (managementobject queryobj in searcher. get () {If (queryobj ["instancename"]. tostring (). contains ("ftdibus") {console. writeline (queryobj ["portname"] + "is a fdtibus device. ") ;}} console. read ();} catch (managementexception e) {system. diagnostics. debug. writeline ("An error occurred while querying for WMI data:" + E. message) ;}}}
In actual use, you can view the attributes you are concerned about in managementobject through debugging and make corresponding judgments.
Refer:Http://www.codeproject.com/Articles/32330/A-Useful-WMI-Tool-How-To-Find-USB-to-Serial-Adapto