Click here to Skip to main content
15,902,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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
    'Program running from USB
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:

VB
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:

VB
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
    'Program running from USB
Else
    'Program running from PC
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"
Posted
Updated 24-Jan-24 7:02am
v2
Comments
Dave Kreskowiak 24-Jan-24 9:35am    
The first problem you're going to find is that USB DeviceId is not as unique as you seem to think it is. That string after the last \ character can be generated by Windows when the device is inserted. Most USB sticks will not include their serial number, if they even have one, in their DeviceId strings, so Windows will generate a random string, which can and will be different from machine to machine for the exact same stick.
BASSIES 24-Jan-24 10:34am    
Thanks for the reply , i give it a try on different windows machine.
Andre Oosthuizen 24-Jan-24 14:36pm    
In the real world scenario I would use version numbers on my app to check which version is running. Dave is totally correct, Windows will generate different numbers for a drive, test it by inserting the same drive twice, different numbers is generated so you will never have a controlled instance on checking if the drive is different from the pc version, as to where your pc version will always be the same, if coded correctly...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900