C# 获取 串口 设备名称 与 串口号 ManagementObjectSearcher类

摘要:
1.效果图:2.代码classProgram{staticvoidMain(string[]args){GetComList();}privatestaticvoidGetComList(){try{using(ManagementObjectSearchersearcher=newManagementObjectSearcher("select*fromWin32_PnPEntity")){Co

1.效果图:

C# 获取 串口 设备名称 与 串口号 ManagementObjectSearcher类第1张

2.代码

classProgram
    {
        static void Main(string[] args)
        {
            GetComList();
        }

        private static voidGetComList()
        {try{
                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_PnPEntity"))
                {
                    Console.WriteLine("本机串口:");

                    var hardInfos =searcher.Get();
                    int index = 1;
                    foreach (var hardInfo inhardInfos)
                    {
                        if (hardInfo.Properties["Name"].Value != null && hardInfo.Properties["Name"].Value.ToString().Contains("(COM"))
                        {
                            String strComName = hardInfo.Properties["Name"].Value.ToString();
                            Console.WriteLine(index + ":" + strComName);//打印串口设备名称及串口号
                            index += 1;
                        }
                    }
                }
                Console.ReadKey();
            }
            catch{

            }
        }
    }

免责声明:文章转载自《C# 获取 串口 设备名称 与 串口号 ManagementObjectSearcher类》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Java调用Python脚本并获取返回值使用adb命令安装安卓apk包下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Educational Codeforces Round 40 (Rated for Div. 2)

A. Diagonal Walking Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants t...

Swift实战-小QQ(第1章):QQ登录界面

1.新建小QQ项目 2.将所需用到的图片资源(resource)文件夹,添加到项目中.并新建一个登录页面:LoginViewController.swift 3.修改LoginViewController.swift代码为 import UIKit class LoginViewController: UIViewController {...

Innosetup 脚本写注册表实现自定义协议(Url Protocol)

[Registry] Root: HKCR; SubKey: NGIE; ValueData: "NGIE"; ValueType: string; Flags: CreateValueIfDoesntExist UninsDeleteKey; Root: HKCR; SubKey: NGIE; ValueName: "URL Protocol";Val...

JS获取当前使用的浏览器名字以及版本号

JS获取当前使用的浏览器名字以及版本号   工作中需要通过JS去获取当前使用的浏览器的名字以及版本号,网上大堆资料都有一个关键词是 navigator.appName,但是这个方法获取的浏览器的名字只有两种要么是IE要么就是Netscap,倒是可以用来判断是否使用了IE,但是我想获取具体的浏览器产品名字比如 Firefox,Chrome等。所以只好通过na...

6Java控制结构

顺序结构 从上到下依次执行,中间没有判断和跳转 比如Java中定义变量中的前向应用,先定义,在使用 分支结构(if, else, switch) 单分支 if // if 的快速入门 import java.util.Scanner; class if01{ public static void main(String[] args) {...

【python 3.6】从网站抓图并存放到本地路径

#!/usr/bin/python # -*- coding: UTF-8 -*- _author_ = 'BH8ANK' import urllib.request import re import os import time #os.rmdir("D:/images") #1,打开页面,读取图片张数,抓html wangzhi = "https...