Obtain the serial device name. Obtain the hardware name in the Device Manager.

Source: Internet
Author: User

Obtain the serial device name. Obtain the hardware name in the Device Manager.
 

Introduction: In a serial communication program, if multiple serial ports are connected at the same time on the PC, it is difficult to know which serial port is used when the serial port is opened from the application program, in this case, you must use the Device Manager to view the serial port name. This code solves this problem and calls the system api to read the serial port device name and serial port number, you do not need to find the serial port from the Device Manager. The key parts of the program are as follows:
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. management; 6 7 namespace StudentSerialPort 8 {9 class GetHardName 10 {11 /// <summary> 12 // enumerate win32 api 13 /// </summary> 14 public enum HardwareEnum 15 {16 // hardware 17 Win32_Processor, // CPU processor 18 Win32_PhysicalMemory, // physical memory 19 Win32_Keyboard, // keyboard 20 Win32_PointingDevice, // point Input Device, Including the mouse. 21 Win32_FloppyDrive, // floppy disk drive 22 Win32_DiskDrive, // hard drive 23 Win32_CDROMDrive, // disc drive 24 Win32_BaseBoard, // motherboard 25 Win32_BIOS, // BIOS chip 26 Win32_ParallelPort, // port 27 Win32_SerialPort, // serial port 28 Win32_SerialPortConfiguration, // serial port configuration 29 Win32_SoundDevice, // multimedia settings, usually the sound card. 30 Win32_SystemSlot, // motherboard slot (ISA & PCI & AGP) 31 Win32_USBController, // USB controller 32 Win32_NetworkAdapter, // network adapter 33 Win32_NetworkAdapterConfiguration, // network adapter setting 34 Win32_Printer, // printer 35 Win32_PrinterConfiguration, // printer setting 36 Win32_PrintJob, // printer Task 37 Win32_TCPIPPrinterPort, // printer port 38 Win32_POTSModem, // MODEM 39 serial, // MODEM Port 40 win32_1_topmonitor, // display 4 1 Win32_DisplayConfiguration, // graphics card 42 Win32_DisplayControllerConfiguration, // graphics card setting 43 Win32_VideoController, // graphics card details. 44 Win32_VideoSettings, // Display Mode Supported by the video card. 45 46 // OS 47 Win32_TimeZone, // Time Zone 48 Win32_SystemDriver, // driver 49 Win32_DiskPartition, // disk partition 50 Win32_LogicalDisk, // Logical Disk 51 Win32_LogicalDiskToPartition, // The partition and start and end position of the Logical Disk. 52 Win32_LogicalMemoryConfiguration, // logic memory configuration 53 Win32_PageFile, // system page file information 54 Win32_PageFileSetting, // page file setting 55 Win32_BootConfiguration, // system startup configuration 56 Win32_ComputerSystem, // brief computer information 57 Win32_OperatingSystem, // OS information 58 Win32_StartupCommand, // the system automatically starts program 59 Win32_Service, // The system installs the service 60 Win32_Group, // System Management Group 61 Win32_GroupUser, // system group account 62 Win32_UserAccount, // user account 63 Win32_Process, // system process 64 Win32_Thread, // system thread 65 Win32_Share, // share 66 Win32_NetworkClient, // installed network client 67 Win32_NetworkProtocol, // installed network protocol 68 Win32_PnPEntity, // all device 69} 70 // <summary> 71 // WMI obtains hardware information 72 /// </summary> 73 /// <param name = "hardType"> </param> 74 // <param name = "propKey"> </param> 75 // <returns> </returns> 76 public static string [] MulGetHardwareInfo (hardwareEnum hardType, string propKey) 77 {78 79 List <string> strs = new List <string> (); 80 try 81 {82 using (ManagementObjectSearcher searcher = new ManagementObjectSearcher ("select * from" + hardType )) 83 {84 var hardInfos = searcher. get (); 85 foreach (var hardInfo in hardInfos) 86 {87 if (hardInfo. properties [propKey]. value! = Null) 88 {89 if (hardInfo. properties [propKey]. value. toString (). contains ("COM") 90 {91 strs. add (hardInfo. properties [propKey]. value. toString (); 92} 93} 94 95 96} 97 searcher. dispose (); 98} 99 return strs. toArray (); 100} 101 catch102 {103 return null; 104} 105 finally106 {strs = null ;} 107} 108 // Use WMI to obtain COM port 109 // <summary> 110 // serial port information 111 /// </summary> 112 // <returns> </ returns> 113 public static string [] GetSerialPort () 114 {115 return MulGetHardwareInfo (HardwareEnum. win32_PnPEntity, "Name"); 116} 117 118 119} 120}
Usage: call API "GetSerialPort" to obtain the device list, as shown below:
1 // get COM Port 2 foreach (string portName in GetHardName. GetSerialPort () 3 {4 toolStripItemCollection. Add (portName); 5} Through WMI}

Effect

 

If an error is reported:

Managementobjectsearcher lacks using

Why does the System. Management prompt that the space is not referenced when ManagementObjectSearcher is used?

Solution:

Add reference... in project to reference System. Management. And then using System. Management.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.