首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Vb.net上检测第一个即插即用usb闪存

如何在Vb.net上检测第一个即插即用usb闪存
EN

Stack Overflow用户
提问于 2017-02-15 20:48:06
回答 2查看 1.4K关注 0票数 1

我试图做一个屏幕储物柜软件,只有当正确的闪存插入和锁定时,它是拔出。所以我搜索并找到了一些侦测闪存的密码。它正常工作时,只有一个闪存盘,但如果有多个闪存驱动器和我拔出一个没有通过,我的软件仍然锁定屏幕。有人能帮忙吗?

这是我的密码

代码语言:javascript
复制
Imports System.Runtime.InteropServices
Imports System.IO

Public Class Form1

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    e.Cancel = True
End Sub

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Me.WindowState = FormWindowState.Maximized
    Me.TopMost = True
End Sub


Private Const WM_DEVICECHANGE As Integer = &H219
Private Const DBT_DEVICEARRIVAL As Integer = &H8000
Private Const DBT_DEVTYP_VOLUME As Integer = &H2
Private Const DBT_DEVICEREMOVECOMPLETE As Integer = &H8004


Public Structure DEV_BROADCAST_HDR
    Public dbch_size As Int32
    Public dbch_devicetype As Int32
    Public dbch_reserved As Int32
End Structure

Private Structure DEV_BROADCAST_VOLUME
    Public dbcv_size As Int32
    Public dbcv_devicetype As Int32
    Public dbcv_reserved As Int32
    Public dbcv_unitmask As Int32
    Public dbcv_flags As Int16
End Structure

Private Function GetDriveLetterFromMask(ByRef Unit As Int32) As Char
    For i As Integer = 0 To 25
        If Unit = (2 ^ i) Then
            Return Chr(Asc("A") + i)
        End If
    Next
End Function


Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

    If m.Msg = WM_DEVICECHANGE Then
        If m.WParam.ToInt32 = DBT_DEVICEARRIVAL Then
            If CInt(m.WParam) = DBT_DEVICEARRIVAL Then
                Dim DeviceInfo As DEV_BROADCAST_HDR
                DeviceInfo = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(DEV_BROADCAST_HDR)), DEV_BROADCAST_HDR)
                If DeviceInfo.dbch_devicetype = DBT_DEVTYP_VOLUME Then
                    Dim Volume As DEV_BROADCAST_VOLUME
                    Volume = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(DEV_BROADCAST_VOLUME)), DEV_BROADCAST_VOLUME)
                    Dim DriveLetter As String = (GetDriveLetterFromMask(Volume.dbcv_unitmask) & ":\")
                    If IO.File.Exists(IO.Path.Combine(DriveLetter, "password.info")) Then


                        Dim fso As Scripting.FileSystemObject
                        Dim oDrive As Scripting.Drive

                        fso = CreateObject("Scripting.FileSystemObject")

                        oDrive = fso.GetDrive(DriveLetter)


                        Dim passline As String() = File.ReadAllLines(DriveLetter & "password.info")

                        If passline(3) = "1120" & oDrive.SerialNumber Then
                            MessageBox.Show("Welcome!")
                            Me.TopMost = False
                            Me.WindowState = FormWindowState.Minimized

                        Else
                            MsgBox("This is not your password.")
                        End If

                    Else

                        MessageBox.Show("Password couldn't be found!")
                    End If
                End If
            End If
        End If
        If m.WParam.ToInt32 = DBT_DEVICEREMOVECOMPLETE Then

            Me.WindowState = FormWindowState.Maximized
            Me.TopMost = True
            MsgBox("Device is removed!")


        End If
    Else

    End If


    MyBase.WndProc(m)
End Sub

End Class
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-16 14:29:19

因为当驱动器被删除时,您无法获得设备ID,所以您无法分辨哪个设备被删除,您所知道的只是删除了一些东西。

在这一点上,你真的需要扫描和看看预期的驱动器是否仍然连接。

当检测到正确的USB驱动器时,需要将其DriveLetter存储在类中,然后在删除驱动器时检查它是否仍然存在。

有点像

代码语言:javascript
复制
    Dim Key_Is_Gone = True
    For Each drv As DriveInfo In My.Computer.FileSystem.Drives
        If drv.Name = DriveLetter Then
            Key_Is_Gone = False
            Exit For 
        End If
    Next

    If Key_Is_Gone Then
         'Do what you have to do
    End If

虽然如果驱动器号确实存在,但您可能需要检查驱动器的其他一些属性,以验证它确实是相同的密钥。否则,一些明亮的火花可能会重新分配驱动器的字母在你身上。

也许吧

代码语言:javascript
复制
drv.RootDirectory.CreationTime

在检测密钥时读取并存储该值,并使用该存储值进行测试。

代码语言:javascript
复制
    Dim Key_Is_Gone = True
    For Each drv As DriveInfo In My.Computer.FileSystem.Drives
        If drv.Name = DriveLetter andalso drv.RootDirectory.CreationTime = DetectedKeyDate Then
            Key_Is_Gone = False
            Exit For 
        End If
    Next

    If Key_Is_Gone Then
         'Do what you have to do
    End If

也是:不要使用FSO,使用本机VB.NET

代码语言:javascript
复制
My.Computer.FileSystem

对象而不是.

票数 0
EN

Stack Overflow用户

发布于 2019-04-09 06:20:20

如果设备是唯一的可移动设备,则可以获取设备类型,然后任意操作

代码语言:javascript
复制
Dim folder = New FolderBrowserDialog()
Dim drives = System.IO.DriveInfo.GetDrives()
Dim usbDrive = drives.FirstOrDefault(Function(m) m.DriveType = System.IO.DriveType.Removable)

For i As Integer = 0 To drives.Count - 1

    If drives(i).DriveType = System.IO.DriveType.Removable Then
       'Codes will not run if there were no removable device
       folder.SelectedPath = usbDrive.RootDirectory.FullName
       MessageBox.Show(folder.SelectedPath)
    End If

Next i

代码用Visual Basic语言编写

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42259734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档