【发布时间】:2014-09-16 16:00:19
【问题描述】:
我是 WinForms 开发人员,我已经知道如何使用 WMI 监控连接或断开连接的 USB,但不久前我发现了现代 Windows 应用程序的 DeviceWatcher 类,它班级第一次感兴趣,因为似乎是一个非常改进和有效的替代方案,可以替代所有解释如何通过 Internet 监控驱动器的“旧”WMI 代码,但直到昨天(感谢this post)我还不知道如何在 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 dw.Added
Dim sb As New System.Text.StringBuilder
With sb
.AppendLine("dw_added")
.AppendLine("********")
.AppendLine(String.Format("Interface ID.: {0}", e.Id))
.AppendLine(String.Format("Friendly Name: {0}", e.Name))
.AppendLine(String.Format("Is Enabled?..: {0}", e.IsEnabled))
End With
Debug.WriteLine(sb.ToString)
End Sub
Private Sub dw_Removed(ByVal sender As DeviceWatcher, ByVal e As DeviceInformationUpdate) _
Handles dw.Removed
Dim sb As New System.Text.StringBuilder
With sb
.AppendLine("dw_Removed")
.AppendLine("**********")
.AppendLine(String.Format("Interface ID:{0}", e.Id))
For Each item As KeyValuePair(Of String, Object) In e.Properties
.AppendLine(String.Format("TKey:{0}, TVal:{1} (TVal Type:{2})",
item.Key, item.Value.ToString, item.Value.GetType.Name))
Next
End With
Debug.WriteLine(sb.ToString)
End Sub
Private Sub dw_Updated(ByVal sender As DeviceWatcher, ByVal e As DeviceInformationUpdate) _
Handles dw.Updated
Dim sb As New System.Text.StringBuilder
With sb
.AppendLine("dw_Updated")
.AppendLine("**********")
.AppendLine(String.Format("Interface ID: {0}", e.Id))
For Each item As KeyValuePair(Of String, Object) In e.Properties
If item.Key.EndsWith("InterfaceEnabled", StringComparison.OrdinalIgnoreCase) Then
Dim Result As Boolean = CBool(item.Value)
' I'm not sure whether the 'Result' value really determines this:
.AppendLine(String.Format("The device is accessible?:{0}", CStr(Result)))
Else
.AppendLine(String.Format("TKwy:{0}, TVal:{1} (TVal Type:{2})",
item.Key, item.Value.ToString, item.Value.GetType.Name))
End If
Next
End With
Debug.WriteLine(sb.ToString)
End Sub
Private Sub dw_Stopped(ByVal sender As DeviceWatcher, ByVal e As Object) _
Handles dw.Stopped
Dim sb As New System.Text.StringBuilder
With sb
.AppendLine("dw_Stopped")
.AppendLine("**********")
.AppendLine(String.Format("e:{1} (e Type:{2})",
e.ToString, e.GetType.Name))
End With
Debug.WriteLine(sb.ToString)
End Sub
Private Sub dw_EnumerationCompleted(ByVal sender As DeviceWatcher, ByVal e As Object) _
Handles dw.EnumerationCompleted
If e IsNot Nothing Then
Dim sb As New System.Text.StringBuilder
With sb
.AppendLine("EnumerationCompleted")
.AppendLine("********************")
.AppendLine(String.Format("e:{1} (e Type:{2})",
e.ToString, e.GetType.Name))
End With
Debug.WriteLine(sb.ToString)
End If
End Sub
End Class
【问题讨论】:
-
这是手机应用还是商店应用?
-
都不是,我已经解释过,这是一个 WindowsForm 项目(桌面应用程序),感谢您的评论!
-
我之所以问,是因为所有相关文档都在 MSDN 上的 Phone 和 StoreApps 下(或者我找到了错误的文档)。对于 WinForms,你可以使用 WMI,不是吗?
-
是的,但我的意图是用现代和改进的方式替换 WMI 的使用,比如 DeviceWatcher(也许我错了,但我认为更好)
-
System.Devices.InterfaceEnabled 将为您提供可用性。要获取设备类型,您可以使用 DeviceInformation.CreateFromIdAsync 方法并指定附加的“System.Devices.InterfaceClassGuid”属性名称(最后一个参数)。 USB 设备将具有此属性的 GUID_DEVINTERFACE_USB_DEVICE。对于其余部分(卷、字母、磁盘等),DeviceWatcher 对您帮助不大。 WMI 对此要简单得多(例如:codeproject.com/Articles/13530/Eject-USB-disks-using-C)。不知道为什么要从 WMI 更改为这个糟糕的 W8/JS 东西...
标签: .net vb.net winforms usb device