C\#직렬 목록 가 져 오기 실시 간 모니터링 직렬

4010 단어 C#주둥이
상용 하 는 두 가지 방법
방법 1:

using Microsoft.Win32; 
  
RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware//DeviceMap//SerialComm"); 
if (keyCom != null) 
{ 
 string[] sSubKeys = keyCom.GetValueNames(); 
 foreach (string sName in sSubKeys) 
 { 
  string sValue = (string)keyCom.GetValue(sName); 
  this.textBox1.Text = this.textBox1.Text + sValue + "/r/n"; 
 } 
} 
방법 2:

using System.IO.Ports; 
 
foreach (string vPortName in SerialPort.GetPortNames()) 
{ 
 this.textBox2.Text = this.textBox2.Text + vPortName + "/r/n"; 
}
봉 인 된 API 를 공유 합 니 다.구현 효 과 는 다음 과 같 습 니 다.
//img.jbzj.com/file_images/article/201605/201654111022644.jpg
코드 는 다음 과 같 습 니 다:

/// <summary>
///   win32 api
/// </summary>
public enum HardwareEnum
{
 //   
 Win32_Processor, // CPU    
 Win32_PhysicalMemory, //      
 Win32_Keyboard, //   
 Win32_PointingDevice, //      ,    。
 Win32_FloppyDrive, //      
 Win32_DiskDrive, //      
 Win32_CDROMDrive, //      
 Win32_BaseBoard, //   
 Win32_BIOS, // BIOS   
 Win32_ParallelPort, //   
 Win32_SerialPort, //   
 Win32_SerialPortConfiguration, //     
 Win32_SoundDevice, //      ,     。
 Win32_SystemSlot, //      (ISA & PCI & AGP)
 Win32_USBController, // USB    
 Win32_NetworkAdapter, //      
 Win32_NetworkAdapterConfiguration, //        
 Win32_Printer, //    
 Win32_PrinterConfiguration, //      
 Win32_PrintJob, //      
 Win32_TCPIPPrinterPort, //      
 Win32_POTSModem, // MODEM
 Win32_POTSModemToSerialPort, // MODEM   
 Win32_DesktopMonitor, //    
 Win32_DisplayConfiguration, //   
 Win32_DisplayControllerConfiguration, //     
 Win32_VideoController, //     。
 Win32_VideoSettings, //          。

 //     
 Win32_TimeZone, //   
 Win32_SystemDriver, //     
 Win32_DiskPartition, //     
 Win32_LogicalDisk, //     
 Win32_LogicalDiskToPartition, //              。
 Win32_LogicalMemoryConfiguration, //       
 Win32_PageFile, //        
 Win32_PageFileSetting, //      
 Win32_BootConfiguration, //       
 Win32_ComputerSystem, //        
 Win32_OperatingSystem, //       
 Win32_StartupCommand, //         
 Win32_Service, //        
 Win32_Group, //      
 Win32_GroupUser, //      
 Win32_UserAccount, //     
 Win32_Process, //     
 Win32_Thread, //     
 Win32_Share, //   
 Win32_NetworkClient, //          
 Win32_NetworkProtocol, //         
 Win32_PnPEntity,//all device
}
/// <summary>
/// WMI     
/// </summary>
/// <param name="hardType"></param>
/// <param name="propKey"></param>
/// <returns></returns>
public static string[] MulGetHardwareInfo(HardwareEnum hardType, string propKey)
{

 List<string> strs = new List<string>();
 try
 {
  using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + hardType))
  {
   var hardInfos = searcher.Get();
   foreach (var hardInfo in hardInfos)
   {
    if (hardInfo.Properties[propKey].Value.ToString().Contains("COM"))
    {
     strs.Add(hardInfo.Properties[propKey].Value.ToString());
    }

   }
   searcher.Dispose();
  }
  return strs.ToArray();
 }
 catch
 {
  return null;
 }
 finally
 { strs = null; }
}
//  WMI  COM  
string[] ss = MulGetHardwareInfo(HardwareEnum.Win32_PnPEntity, "Name");

좋은 웹페이지 즐겨찾기