代码之家  ›  专栏  ›  技术社区  ›  kenny

蓝牙串行设备检测COM端口#

  •  1
  • kenny  · 技术社区  · 15 年前

    1 回复  |  直到 15 年前
        1
  •  0
  •   kenny    15 年前

    我刚刚发现下面的C#WMI代码似乎可以完成这项工作。但这可靠吗?

            string deviceid = string.Empty;
            ManagementObjectSearcher searcher = new ManagementObjectSearcher ( "Select * from WIN32_SerialPort" );
            foreach ( ManagementObject port in searcher.Get () )
            {
                string name = port.GetPropertyValue ("Name" ) as string;
                string provider = port.GetPropertyValue ( "ProviderType" ) as string;
                if ( provider == null )
                {
                    deviceid = port.GetPropertyValue ( "DeviceId" ) as string;
                }
            }