TheInsider
Programmer
Hi,
I'm new to VB.Net, so I'm probably not understanding a basic concept here.
I found some code (below) that will trigger an event whenever someone plugs in a USB drive. I need this check to be running constantly in the background. When a device is plugged in, I want to perform an action like enable a button or open another form.
The code works, except I get a
"A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll"
error if I try to change any of control properties or do just about anything else.
You can test this code by simply creating a form with a button on it called "Button1". Right now it's set up to display a message box when a pen drive is plugged in, and then on the next line, it should try to disable the command button. The latter is where the program crashes.
I'm pulling my hair out. Obviously, an event is useless if you can't take any action when it's triggered. Any help is much appreciated!!!!!!
TIA
PS. I'm not sure if the GetDriveLetterFromDisk() method is part of the problem or not, so I included it anyway.
I'm new to VB.Net, so I'm probably not understanding a basic concept here.
I found some code (below) that will trigger an event whenever someone plugs in a USB drive. I need this check to be running constantly in the background. When a device is plugged in, I want to perform an action like enable a button or open another form.
The code works, except I get a
"A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll"
error if I try to change any of control properties or do just about anything else.
You can test this code by simply creating a form with a button on it called "Button1". Right now it's set up to display a message box when a pen drive is plugged in, and then on the next line, it should try to disable the command button. The latter is where the program crashes.
I'm pulling my hair out. Obviously, an event is useless if you can't take any action when it's triggered. Any help is much appreciated!!!!!!
TIA
PS. I'm not sure if the GetDriveLetterFromDisk() method is part of the problem or not, so I included it anyway.
Code:
Imports System.Management
Public Class Form1
Private WithEvents m_MediaConnectWatcher As ManagementEventWatcher
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")
Button1.Enabled = True
End If
Case "__InstanceDeletionEvent"
If obj("InterfaceType") = "USB" Then
MsgBox(obj("Caption") & " has been unplugged")
Button1.Enabled = False
End If
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_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
m_MediaConnectWatcher.Stop()
End Sub