usb插入/拔出系统和其他操作 [英] usb plugged/unplugged from the system and other actions
问题描述
我的应用是在任何USB设备从系统插入或拔出时发送电子邮件。我使用ManagementEventWatcher来捕获此类事件。毫无疑问,我们遇到了一些情况,即usb驱动器出现了问题,并且它在某种程度上从系统中消失了
(不是物理上未插入)。我们没有注意到任何通知意味着事件似乎不在我们已经使用的内容之下。有什么东西我可以添加到我的应用程序来处理其他事情,而不是像usb
电源问题或其他任何相关的插件/拔出?请参阅下面的代码:
I have appliction that send email when any of usb devices is plugged or unplugged from the system. I use ManagementEventWatcher to catch such event. Neverthless we faced some situation that there was something wrong with usb drive and it was somehow dissapered from the system (not physically unplugged). We haven't noticed any notification means it seems that event is not under what we already using. Is there anything that i could add to my application to handle other things instead of justus plug/unplug like usb power issue or whatever else regarding that? See my code below:
Imports System.Management
Public Class Usb
Public Event ReportEvent(information As String)
Private WithEvents _mediaConnectWatcher As ManagementEventWatcher
Private _usbDriveName As String
Private _uUsbDriveLetter As String
Private Enum InterfaceDriveType
Usb
End Enum
Private Async Function StartDetection() As Task
' __InstanceOperationEvent will trap both Creation and Deletion of class instances
Dim query2 As New WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 1 " _
& "WHERE TargetInstance ISA 'Win32_DiskDrive'")
_mediaConnectWatcher = New ManagementEventWatcher
_mediaConnectWatcher.Query = query2
'AddHandler _mMediaConnectWatcher.EventArrived, AddressOf Arrived
Dim t As Task= Task.Run(sub() _mediaConnectWatcher.Start())
Await t
End Function
Public sub Arrived(sender As Object, e As EventArrivedEventArgs) Handles _mediaConnectWatcher.EventArrived
Try
'If InvokeRequired Then
' Invoke(New Action(Of Object, EventArrivedEventArgs)(AddressOf Arrived), sender, e)
'Else
Dim mbo, obj As ManagementBaseObject
'the first thing we have to do is figure out if this is a creation or deletion event
mbo = e.NewEvent
'next we need a copy of the instance that was either created or deleted
obj = CType(mbo("TargetInstance"), ManagementBaseObject)
推荐答案
JimmyJimm,
Hi JimmyJimm,
谢谢你r发布在这里。
对于你的问题,你可以尝试下面的代码。
For your question, you could try the code below.
Imports System.Management ' Also had to add System.Management as a reference
Public Class Form1
Private WithEvents m_MediaConnectWatcher As ManagementEventWatcher
Public USBDriveName As String
Public USBDriveLetter As String
Public Sub StartDetection()
' __InstanceOperationEvent will trap both Creation and Deletion of class instances
Dim query2 As New WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 1 " _
& "WHERE TargetInstance ISA 'Win32_DiskDrive'")
m_MediaConnectWatcher = New ManagementEventWatcher
m_MediaConnectWatcher.Query = query2
m_MediaConnectWatcher.Start()
End Sub
Private Sub Arrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs) Handles m_MediaConnectWatcher.EventArrived
Dim mbo, obj As ManagementBaseObject
' the first thing we have to do is figure out if this is a creation or deletion event
mbo = CType(e.NewEvent, ManagementBaseObject)
' next we need a copy of the instance that was either created or deleted
obj = CType(mbo("TargetInstance"), ManagementBaseObject)
Select Case mbo.ClassPath.ClassName
Case "__InstanceCreationEvent"
If obj("InterfaceType") = "USB" Then
MsgBox(obj("Caption") & " (Drive letter " & GetDriveLetterFromDisk(obj("Name")) & ") has been plugged in")
Else
MsgBox(obj("InterfaceType"))
End If
Case "__InstanceDeletionEvent"
If obj("InterfaceType") = "USB" Then
MsgBox(obj("Caption") & " has been unplugged")
If obj("Caption") = USBDriveName Then
USBDriveLetter = ""
USBDriveName = ""
End If
Else
MsgBox(obj("InterfaceType"))
End If
Case Else
MsgBox("nope: " & obj("Caption"))
End Select
End Sub
Private Function GetDriveLetterFromDisk(ByVal Name As String) As String
Dim oq_part, oq_disk As ObjectQuery
Dim mos_part, mos_disk As ManagementObjectSearcher
Dim obj_part, obj_disk As ManagementObject
Dim ans As String = ""
' WMI queries use the "\" as an escape charcter
Name = Replace(Name, "\", "\\")
' First we map the Win32_DiskDrive instance with the association called
' Win32_DiskDriveToDiskPartition. Then we map the Win23_DiskPartion
' instance with the assocation called Win32_LogicalDiskToPartition
oq_part = New ObjectQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & Name & """} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
mos_part = New ManagementObjectSearcher(oq_part)
For Each obj_part In mos_part.Get()
oq_disk = New ObjectQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & obj_part("DeviceID") & """} WHERE AssocClass = Win32_LogicalDiskToPartition")
mos_disk = New ManagementObjectSearcher(oq_disk)
For Each obj_disk In mos_disk.Get()
ans &= obj_disk("Name") & ","
Next
Next
Return ans.Trim(","c)
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
StartDetection()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
m_MediaConnectWatcher.Stop()
Application.Exit()
End Sub
End Class
最好的问候,
Wendy
这篇关于usb插入/拔出系统和其他操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!