Click here to Skip to main content
15,914,163 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What I would like to do is identify my pen driver and allow the form to open only if the exe is inside it.

What I have tried:

Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher("SELECT * FROM Win32_volume")
       Dim drive As Integer
       For Each wmi_USB As ManagementObject In searcher.Get()
           If (wmi_USB("SerialNumber")) = "1820527542" Then
               drive = 1
               Exit For
           End If
       Next
       If Not drive = 1 Then
           MsgBox("Error - The pendriver was not found!", MsgBoxStyle.Critical, "title")
           Me.Close()
       End If
VB

Posted
Updated 1-Mar-18 3:25am
v3

1 solution

I'd suggest to use DriveInfo Class (System.IO)[^]

How to get list of removable drives?
VB.NET
Dim RemoveableDrives = DriveInfo.GetDrives().Where(Function(x) x.DriveType=DriveType.Removable).ToList()
For Each d As DriveInfo In RemoveableDrives
    Console.WriteLine(" Drive name: {0} type: {1}", d.Name, d.DriveType)
    If d.IsReady = True Then
        Console.WriteLine("  Volume label: {0}", d.VolumeLabel)
        Console.WriteLine("  File system: {0}", d.DriveFormat)
        Console.WriteLine("  Available space to current user:{0, 15} bytes", d.AvailableFreeSpace)
        Console.WriteLine("  Total available space: {0, 15} bytes", d.TotalFreeSpace)
        Console.WriteLine("  Total size of drive: {0, 15} bytes ", d.TotalSize)
    End If
Next


Now, you have to check if drive of Application.StartupPath Property (System.Windows.Forms)[^] is a removable drive.
 
Share this answer
 
Comments
RafaelFranckilin 1-Mar-18 9:26am    
In fact I want to prevent someone from copying my program to another pendriver, so I need to identify my pendriver and prevent the program from running on another device other than my pendriver.
Maciej Los 1-Mar-18 9:27am    
And...

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