插入时自动扫描USB(禁用或启用) [英] Auto Scan USB when you plug (Disable or Enable)

查看:79
本文介绍了插入时自动扫描USB(禁用或启用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的想法是,当您将USB插入计算机时,计算机将获得USB的信息.如果数据库中有该信息,则计算机将获得USB.如果不在数据库中,则计算机将无法获得USB.

救救我!谢谢!

My idea is when you plug USB into the computer, the computer will get information of USB. If you have that information in the database, the computer will get USB. If not in the database, the computer does not get the USB.

Hepl me! Thanks!

推荐答案

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

   Private Structure DEV_BROADCAST_VOLUME
        Dim Dbcv_Size As Integer
       Dim Dbcv_Devicetype As Integer
       Dim Dbcv_Reserved As Integer
       Dim Dbcv_Unitmask As Integer
       Dim Dbcv_Flags As Short
   End Structure




   Protected Overrides Sub WndProc(ByRef M As System.Windows.Forms.Message)
       On Error Resume Next

       If M.Msg = WM_DEVICECHANGE Then
           Select Case M.WParam
               Case DBT_DEVICEARRIVAL
                   Dim DevType As Integer = Marshal.ReadInt32(M.LParam, 4)
                   If DevType = DBT_DEVTYP_VOLUME Then
                       Dim Vol As New DEV_BROADCAST_VOLUME
                       Vol = Marshal.PtrToStructure(M.LParam, GetType(DEV_BROADCAST_VOLUME))

                       If Vol.Dbcv_Flags = 0 Then
                           For i = 0 To 20
                               If Math.Pow(2, i) = Vol.Dbcv_Unitmask Then
                                   Dim Usb As String = Chr(65 + i) + ":\"
                                   Msgbox (Usb)
                                   Exit For
                               End If
                           Next
                       End If
                   End If
           End Select
       End If
       MyBase.WndProc(M)
   End Sub


您不能编写C#或VB.NET代码以拒绝" USB驱动器.添加驱动器时,会在系统级别触发事件,您可以使用System.IO.Drives枚举驱动器,但不能阻止类似AFAIK这样的USB驱动器,而不是.NET.
You can''t write C# or VB.NET code to ''reject'' a USB drive. There are events that fire on the system level when a drive is added, and you can enumerate drives using System.IO.Drives, but you can''t block a USB drive like that, AFAIK, not in .NET.


您的想法真的很棒,如果您完成了该程序,能否与我们分享? :)
Your idea is really great, if you are done doing that program, can you please share it with us? :)


这篇关于插入时自动扫描USB(禁用或启用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆