C# 如何枚举设备?

C# 如何枚举设备?,c#,winforms,C#,Winforms,我正在尝试连接蓝牙设备(RFComm)。 首先,我想我需要获取设备列表,所以我尝试使用Windows.Devices.Enumeration VS2019在编辑代码时不显示任何错误。但当我开始运行它时,VS在输出中显示: error CS1545: Property, indexer, or event 'DeviceWatcher.Added' is not supported by the language; try directly calling accessor methods 'De

我正在尝试连接蓝牙设备(RFComm)。 首先,我想我需要获取设备列表,所以我尝试使用Windows.Devices.Enumeration

VS2019在编辑代码时不显示任何错误。但当我开始运行它时,VS在输出中显示:

error CS1545: Property, indexer, or event 'DeviceWatcher.Added' is not supported by the language; try directly calling accessor methods 'DeviceWatcher.add_Added(TypedEventHandler<DeviceWatcher, DeviceInformation>)' or 'DeviceWatcher.remove_Added(EventRegistrationToken)'

:“您唯一应该在标题中使用标记的时间是当它们与标题的对话基调有机时。”-我已为您删除了问题标题中的标记。
DeviceInformation
属于
Windows.Devices.Enumeration
,一个UWP程序集。您可以创建一个来初始化
CIMV2
范围内具有通用
TargetInstance
的a,并订阅事件。此处的示例代码:否。您必须添加对
System.Management
的项目引用,当然,还必须在使用这些对象的类之上使用System.Management

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            deviceWatcher = DeviceInformation.CreateWatcher();

            deviceWatcher.Added += onAdded;

            deviceWatcher.Start();
        }

        private DeviceWatcher deviceWatcher = null;

        public void onAdded(DeviceWatcher dw, DeviceInformation di)
        {
            this.listBox1.Items.Add(di.Id.ToString() + "_" + di.Name);
        }

    }