我是一名WinForms开发人员,我已经知道如何使用WMI监控连接或断开连接的USB,但是之前我发现了适用于Modern Windows Apps 的DeviceWatcher类,该类第一次感兴趣,因为它看起来很像改进和有效的替代方案来替换所有那些解释如何通过Internet监控驱动器的"旧"WMI代码,但直到昨天(感谢这篇文章)我还不知道如何在WinForms项目中使用DeviceWatcher,但现在我在WinForms项目中使用DeviceWatcher.
问题是,也许我错了,但我认为这不是我所期望的,只是我找不到任何关于DeviceWatcher的文档(只有上面的MSDN示例),我找不到通往检索必要的信息来监控驱动器事件,我试图处理DeviceWatcher的所有事件,在Debug控制台中打印出参数中包含的所有数据,希望找到可以帮助我的东西......但是不,我非常坚持使用DeviceWatcher类,我无法想象如何进行.
当我连接或断开USB时,我只看到两件事,硬件ID和'InterfaceEnabled'属性(我不知道它是否决定了设备可用性),没有什么有趣的.
我所取得的成就:
·检索硬件设备ID.
我想要完成的事情:
·在连接,断开和断开设备时,检索设备类型(区分USB和其他类型的设备).
·设备连接,断开连接和断开连接时,检索设备可用性(我的意思是设备是否可以读取/写入数据).
·在连接,断开和断开设备时检索设备字母.
·在连接,断开和断开设备时检索设备标签描述.
代码:
Public Class DeviceWatcher_Test
Private WithEvents dw As DeviceWatcher = DeviceInformation.CreateWatcher
' It's suposed that these properties should exist in the "e.properties" on the "dw_updated" event?, not in my case.
' Dim props As String() = {"System.ItemNameDisplay", "System.Devices.ModelName", "System.Devices.Connected"}
Private Sub Test() Handles MyBase.Load
dw.Start()
End Sub
Private Sub dw_Added(ByVal sender As DeviceWatcher, ByVal e As DeviceInformation) _
Handles …Run Code Online (Sandbox Code Playgroud)