|
-
Dec 1st, 2008, 05:18 PM
#1
Thread Starter
Frenzied Member
[2008] Detect the insertion of a USB mass storage device (USB stick etc.)
I'm looking for a way to detect when a new USB mass storage device is inserted in one of the pc's USB ports... I need to get it's drive letter and check for the existence of a particular file. Any ideas?
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Dec 2nd, 2008, 01:16 AM
#2
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Here are the results from Google.
-
Dec 2nd, 2008, 09:22 AM
#3
Hyperactive Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Here is a VERY lame way to do it. Just put this in a Timer_Tick function:
Code:
Dim oDrive As DriveInfo
For Each oDrive In DriveInfo.GetDrives
If oDrive.DriveType = IO.DriveType.Removable Then
MessageBox.Show("Removable USB Drive " & oDrive.Name & " found.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification, False)
Exit Sub
End If
Next
You can also use WMI like this (This is the entire form class):
Code:
Imports System.Management
Public Class Form1
Private WithEvents m_MediaConnectWatcher As ManagementEventWatcher
Public USBDriveName As String
Public USBDriveLetter As String
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
StartDetection()
End Sub
Public Sub StartDetection()
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 As ManagementBaseObject
Dim obj As ManagementBaseObject
mbo = CType(e.NewEvent, ManagementBaseObject)
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")
End If
Case "__InstanceDeletionEvent"
If obj("InterfaceType") = "USB" Then
MsgBox(GetDriveLetterFromDisk(obj("Name")))
If obj("Caption") = USBDriveName Then
USBDriveLetter = ""
USBDriveName = ""
End If
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 = ""
Name = Replace(Name, "\", "\\")
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
End Class
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Dec 2nd, 2008, 09:41 AM
#4
Thread Starter
Frenzied Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Thanks to both of you. I'll try out the code as soon as I can and post accordingly.
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Dec 2nd, 2008, 12:02 PM
#5
Thread Starter
Frenzied Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Ok after adding references to System.Management and System.Management.Instrumentation, I made some changes to the code, in order to fix several errors (concatenation operator cannot be used on Object etc.)
Code:
Imports System.Management
Public Class Form3
Private WithEvents m_MediaConnectWatcher As ManagementEventWatcher
Public USBDriveName As String
Public USBDriveLetter As String
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
StartDetection()
End Sub
Private Sub Form3_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
m_MediaConnectWatcher.Stop()
m_MediaConnectWatcher.Dispose()
End Sub
Public Sub StartDetection()
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 As ManagementBaseObject
Dim obj As ManagementBaseObject
mbo = CType(e.NewEvent, ManagementBaseObject)
obj = CType(mbo("TargetInstance"), ManagementBaseObject)
Select Case mbo.ClassPath.ClassName
Case "__InstanceCreationEvent"
If obj.Item("InterfaceType").ToString = "USB" Then
USBDriveName = obj.Item("Caption").ToString
USBDriveLetter = GetDriveLetterFromDisk(obj.Item("Name").ToString)
MessageBox.Show(USBDriveName & " (Drive letter " & USBDriveLetter & ") has been plugged in")
End If
Case "__InstanceDeletionEvent"
If obj.Item("InterfaceType").ToString = "USB" Then
MessageBox.Show(USBDriveName & " was disconnected. " & USBDriveLetter & " is now inaccessible.") 'GetDriveLetterFromDisk(obj.Item("Name").ToString))
If obj.Item("Caption").ToString = USBDriveName Then
USBDriveLetter = ""
USBDriveName = ""
End If
End If
Case Else
MessageBox.Show("nope: " & obj.Item("Caption").ToString)
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 = ""
Name = Replace(Name, "\", "\\")
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.Item("DeviceID").ToString & """} WHERE AssocClass = Win32_LogicalDiskToPartition")
mos_disk = New ManagementObjectSearcher(oq_disk)
For Each obj_disk In mos_disk.Get()
ans &= obj_disk.Item("Name").ToString & ","
Next
Next
Return ans.Trim(","c)
End Function
Please note that the ManagementEventWatcher must be disposed of when the form is closed, otherwise an exception is thrown.
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Dec 2nd, 2008, 06:48 PM
#6
Thread Starter
Frenzied Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
One more question... Would this same code also detect the insertion of a memory card in a card reader? If not, would it be possible to make some changes so that it does?
Last edited by obi1kenobi; Dec 2nd, 2008 at 06:55 PM.
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Dec 3rd, 2008, 07:34 AM
#7
Hyperactive Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
I'm not 100% sure if it would. It would probably depend on the reader. I would guess it would if the reader was USB external, but for internal readers maybe/maybe not.
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Dec 3rd, 2008, 10:35 AM
#8
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
 Originally Posted by .NetNinja
I'm not 100% sure if it would. It would probably depend on the reader. I would guess it would if the reader was USB external, but for internal readers maybe/maybe not.
Just to let you guys know, both internal and external usb readers hook to the system's usb bus, meaning internal readers are hooked to the usb hub built into the motherboard/usb expansion card on the system.
-
Dec 3rd, 2008, 10:52 AM
#9
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
There are some chip readers that have a permanent "drive assignment" in Windows (just like a CDROM does, whether it has a disk in it or not) and they don't recognize the act of chip insertion or removal. Those may cause problems.
-
Dec 3rd, 2008, 11:07 AM
#10
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
 Originally Posted by Jenner
There are some chip readers that have a permanent "drive assignment" in Windows (just like a CDROM does, whether it has a disk in it or not) and they don't recognize the act of chip insertion or removal. Those may cause problems.
Both of my card readers (one internal and one external that plugs into a usb port on the usb hub in my dell monitor) both have an assigned windows drive letter (Once the external one's plugged windows assigns it the drive letters) for each reader (4 each) and when I insert my sd card windows xp some how detects it and pops up the 'What do you want to do with it" window immediately after insertion of the card.
Windows Vista pops up with the same window 5 minutes after the card's inserted. Then again Vista wasn't built to get work done, so 5 minute delays are excusable to the public.
Last edited by JuggaloBrotha; Dec 4th, 2008 at 09:04 AM.
-
Dec 3rd, 2008, 05:38 PM
#11
Thread Starter
Frenzied Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Well my card reader doesn't have a permanent drive assignment, so is it safe to assume that the code will work as is? I do not own a card so I cannot try it myself... :/
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Dec 4th, 2008, 07:32 AM
#12
Hyperactive Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
I am not sure I would say it is safe to assume. I am fairly certain that is the case though.
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Dec 10th, 2008, 01:16 PM
#13
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
If the above code is working, perhaps a codebank thread would be in order?
-
Dec 10th, 2008, 03:14 PM
#14
Fanatic Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
This is a great thread. Subscribing.
-
Dec 17th, 2008, 07:21 AM
#15
Thread Starter
Frenzied Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Sorry for being absent for so long a period, I had no access to the internet for over 10 days :S
The code works like a charm, no issues as of this moment. Since I didn't provide the code in the first place, I only tweaked it to work better, I believe it would be more appropriate if .NetNinja posted this in the CodeBank... After all, I don't want to take credit for something someone else did...
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Dec 17th, 2008, 07:43 AM
#16
Hyperactive Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
 Originally Posted by obi1kenobi
Sorry for being absent for so long a period, I had no access to the internet for over 10 days :S
The code works like a charm, no issues as of this moment. Since I didn't provide the code in the first place, I only tweaked it to work better, I believe it would be more appropriate if .NetNinja posted this in the CodeBank... After all, I don't want to take credit for something someone else did... 
I have added this to the code bank: http://www.vbforums.com/showthread.php?t=550730
obi1kenobi: Thanks again for the tweak.
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Dec 17th, 2008, 04:12 PM
#17
Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Code:
Error 1 Type 'ManagementEventWatcher' is not defined.
Error 2 Type 'WqlEventQuery' is not defined.
Error 3 Type 'ManagementEventWatcher' is not defined.
Error 4 Type 'System.Management.EventArrivedEventArgs' is not defined.
Error 5 Type 'ManagementBaseObject' is not defined.
Error 6 Type 'ManagementBaseObject' is not defined.
Error 7 Type 'ManagementBaseObject' is not defined.
Error 8 Type 'ManagementBaseObject' is not defined.
Error 9 Type 'ObjectQuery' is not defined.
Error 10 Type 'ManagementObjectSearcher' is not defined.
Error 11 Type 'ManagementObject' is not defined.
Error 12 Type 'ObjectQuery' is not defined.
Error 13 Type 'ManagementObjectSearcher' is not defined.
-
Dec 17th, 2008, 04:17 PM
#18
Hyperactive Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Did you import System.Management?
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Jan 2nd, 2009, 05:37 PM
#19
Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
how to remove msgbox and show result in TextBox ?
-
Jan 2nd, 2009, 06:04 PM
#20
Thread Starter
Frenzied Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Remove the "MessageBox.Show" part and replace it with "TextBox1.Text =" - no quotation marks.
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Jan 2nd, 2009, 06:16 PM
#21
Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
obi1kenobi thnx for reply
i try but doesn't work, did nothing !!
any idea !
-
Jan 4th, 2009, 12:20 PM
#22
Thread Starter
Frenzied Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Perhaps the name of the TextBox you are using is different? I can't think of another reason which would prevent it from working. Post your code and I'll fix it for you.
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Jan 4th, 2009, 12:31 PM
#23
Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Code:
Imports System.Management
Public Class Form1
Private WithEvents m_MediaConnectWatcher As ManagementEventWatcher
Public USBDriveName As String
Public USBDriveLetter As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
StartDetection()
End Sub
Public Sub StartDetection()
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 As ManagementBaseObject
Dim obj As ManagementBaseObject
mbo = CType(e.NewEvent, ManagementBaseObject)
obj = CType(mbo("TargetInstance"), ManagementBaseObject)
Select mbo.ClassPath.ClassName
Case "__InstanceCreationEvent"
If obj.Item("InterfaceType").ToString = "USB" Then
USBDriveName = obj.Item("Caption").ToString
USBDriveLetter = GetDriveLetterFromDisk(obj.Item("Name").ToString)
TextBox1.Text = (USBDriveName & " (Drive letter " & USBDriveLetter & ") has been plugged in")
End If
Case "__InstanceDeletionEvent"
If obj.Item("InterfaceType").ToString = "USB" Then
TextBox1.Text = (USBDriveName & " was disconnected. " & USBDriveLetter & " is now inaccessible.") 'GetDriveLetterFromDisk(obj.Item("Name").ToString))
If obj.Item("Caption").ToString = USBDriveName Then
USBDriveLetter = ""
USBDriveName = ""
End If
End If
Case Else
TextBox1.Text = ("nope: " & obj.Item("Caption").ToString)
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 = ""
Name = Replace(Name, "\", "\\")
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.Item("DeviceID").ToString & """} WHERE AssocClass = Win32_LogicalDiskToPartition")
mos_disk = New ManagementObjectSearcher(oq_disk)
For Each obj_disk In mos_disk.Get()
ans &= obj_disk.Item("Name").ToString & ","
Next
Next
Return ans.Trim(","c)
End Function
End Class
doesn't work for me !!!!
-
Jan 7th, 2009, 08:21 AM
#24
Hyperactive Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Why do you have :
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_
Handles MyBase.Load
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Jan 7th, 2009, 08:34 AM
#25
Thread Starter
Frenzied Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
@.NetNinja: That's the correct event signature, check your IDE.
@Biggy-D: The code you are using is ok. Add the reference to System.Management and it'll work fine. Right-click your project in solution explorer, click Properties, select the References tab, then click Add and in the .Net tab scroll to System.Management and select it. The errors will disappear.
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Jan 8th, 2009, 08:27 AM
#26
Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
@obi1kenobi
the reference :System.Management is already inserted ..
the code doesn't give errors,but doesn't work when i change " messagebox to textbox.text " in the textbox.text i want to display drive info, like messagebox info.
-
Jan 8th, 2009, 09:24 AM
#27
Thread Starter
Frenzied Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
I tried it in 2005 and it indeed didn't work, I got an InvalidCOMException. I'm puzzled, it should have worked XC
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Jan 8th, 2009, 09:34 AM
#28
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
What did you try that did not work?
-
Jan 8th, 2009, 11:03 AM
#29
Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
-
Jan 8th, 2009, 11:06 AM
#30
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
What did you mean by it doesn't work? It doesn't detect or are you encountering some exceptions?
-
Jan 8th, 2009, 11:12 AM
#31
Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
ok. i try to change
Code:
MessageBox.Show(USBDriveName & " (Drive letter " & USBDriveLetter & ") has been plugged in")
to
Code:
TextBox1.text = (USBDriveName & " (Drive letter " & USBDriveLetter & ") has been plugged in")
in textbox1 doesn't show result..
Last edited by Biggy-D; Jan 8th, 2009 at 11:21 AM.
-
Jan 8th, 2009, 11:15 AM
#32
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
When using the MessageBox does it get the correct result?
-
Jan 8th, 2009, 11:17 AM
#33
Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
when using the messagebox i get the correct result..
-
Jan 8th, 2009, 11:36 AM
#34
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
That's odd. Care to post the relevant section of the code where you are using a textbox?
-
Jan 8th, 2009, 11:49 AM
#35
Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
textbox1 is in a form1, my code is under form1 class .
here is my code :
vb Code:
Imports System.Management Public Class Form1 Private WithEvents m_MediaConnectWatcher As ManagementEventWatcher Public USBDriveName As String Public USBDriveLetter As String Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load StartDetection() End Sub Public Sub StartDetection() 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 As ManagementBaseObject Dim obj As ManagementBaseObject mbo = CType(e.NewEvent, ManagementBaseObject) obj = CType(mbo("TargetInstance"), ManagementBaseObject) Select mbo.ClassPath.ClassName Case "__InstanceCreationEvent" If obj.Item("InterfaceType").ToString = "USB" Then USBDriveName = obj.Item("Caption").ToString USBDriveLetter = GetDriveLetterFromDisk(obj.Item("Name").ToString) TextBox1.Text = (USBDriveName & " (Drive letter " & USBDriveLetter & ") has been plugged in") End If Case "__InstanceDeletionEvent" If obj.Item("InterfaceType").ToString = "USB" Then TextBox1.Text = (USBDriveName & " was disconnected. " & USBDriveLetter & " is now inaccessible.") 'GetDriveLetterFromDisk(obj.Item("Name").ToString)) If obj.Item("Caption").ToString = USBDriveName Then USBDriveLetter = "" USBDriveName = "" End If End If Case Else TextBox1.Text = ("nope: " & obj.Item("Caption").ToString) 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 = "" Name = Replace(Name, "\", "\\") 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.Item("DeviceID").ToString & """} WHERE AssocClass = Win32_LogicalDiskToPartition") mos_disk = New ManagementObjectSearcher(oq_disk) For Each obj_disk In mos_disk.Get() ans &= obj_disk.Item("Name").ToString & "," Next Next Return ans.Trim(","c) End Function End Class
-
Jan 9th, 2009, 02:08 PM
#36
Hyperactive Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
Could it have something to do with Cross Threading? (I could be way off)
"Don't try to be a great man. Just be a man and let history make its own judgement."
-
Jan 9th, 2009, 03:42 PM
#37
Thread Starter
Frenzied Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
I tried his code in 2005 and it just ignored the TextBox1.Text line and then threw the exception I described... I can't make heads or tails of this problem...
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
-
Jan 9th, 2009, 05:40 PM
#38
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
It could be a problem with WMI, when I have time I will try to dig furhter into it.
-
Jan 10th, 2009, 10:11 AM
#39
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
I know I already pointed this out in the codebank entry but it may just be worth mentioning here if you guys are having problems with the WMI version : I made something that does pretty much the exact same thing obi1 described in his original post (ie detect when a new drive is inserted and check for a file on it) using Windows API.
Read all 7 posts in this thread (The 2nd post provides the full code but then I fixed a little problem with the drive letter assignment in the last post) and see if it helps: http://www.vbforums.com/showthread.php?t=534956
Oh and the way I'm searching for the file in that 2nd post isnt great lol I dont know why I was using GetFiles instead of FileExists... it still works but it could be slow if the USB device has a lot of files on it so you might want to change that bit (in the WndProc sub)
Last edited by chris128; Jan 10th, 2009 at 10:15 AM.
-
Jan 11th, 2009, 09:02 AM
#40
Member
Re: [2008] Detect the insertion of a USB mass storage device (USB stick etc.)
chris128
the WMI version work for me !
thnx
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|