I want to combine this code: If the USB present or not with certain number, and is the programm running from that USB drive.
With this I get the USB numbers, and check if it is present:
Dim USBClass As New System.Management.ManagementClass("Win32_USBHub")
Dim USBCollection As System.Management.ManagementObjectCollection = USBClass.GetInstances()
Dim USB As System.Management.ManagementObject
Dim allDrives() As IO.DriveInfo = IO.DriveInfo.GetDrives()
Dim d As IO.DriveInfo
Dim MyDrive As String = Path.GetPathRoot(Application.StartupPath)
If GetDriveType(MyDrive) = DriveType.Removable Then
Else
MessageBox.Show("Sorry.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
For Each USB In USBCollection
Me.ListBox1.Items.Add("Device ID = " & USB("deviceid").ToString())
Next USB
Dim d As IO.DriveInfo
For Each d In allDrives
If d.IsReady = True AndAlso d.DriveType = IO.DriveType.Removable Then
ListBox1.Items.Add(d.VolumeLabel).ToString()
If IO.DriveType.Removable Then
End If
Else
If d.IsReady = True And Not d.DriveType = DriveType.Removable Then
ListBox1.Items.Add(d.RootDirectory).ToString()
End If
End If
Next
Check if it is there:
Dim found As String = ""
Dim foo As String
foo = "Device ID = USB\VID_0951&PID_1666\4CEDFB74A3A4F791091722"
For i As Integer = 0 To Keuze.ListBox1.Items.Count - 1
If Keuze.ListBox1.Items(i).ToString = foo Then
found = (i)
End If
Next
If found = "" Then
MessageBox.Show("Sorry", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("Jep!")
End If
That works.
And this for running on USB or not:
Public Function GetDriveType(ByVal Drive As String) As DriveType
Dim MyDrive As New DriveInfo(Drive)
Return MyDrive.DriveType
End Function
im MyDrive As String = Path.GetPathRoot(Application.StartupPath)
If GetDriveType(MyDrive) = DriveType.Removable Then
Else
End If
The above code is for:
Is the USB present with the right number.
and is it running from USB.
But if I put the programm on a second USB and leave the first USB with the rigt number it works also. Because it is running from the second USB and the right number is also there on a other USB port.
The Question
How do I check if programm is running on this USB?
foo = "Device ID = USB\VID_0951&PID_1666\4CEDFB74A3A4F791091722"
What I have tried:
Both code's are working but want to combine these 2. Is it running from this USB
"Device ID = USB\VID_0951&PID_1666\4CEDFB74A3A4F791091722"