无法检测到 USB [英] Failed to detect USB

查看:40
本文介绍了无法检测到 USB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,它可以检测连接到计算机的 USB 设备.我在网上搜索后得到如下代码:

I am working on a application which should detect USB devices connected to the Computer. I got the following code after searching on the Internet:

Public Class Form1

Private Const WM_DEVICECHANGE As Integer = &H219
Private Const DBT_DEVICEARRIVAL As Integer = 32768

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    If m.Msg = WM_DEVICECHANGE Then
        If m.WParam = DBT_DEVICEARRIVAL Then
            MessageBox.Show("Removable Drive Detected")
        End If
    End If
    MyBase.WndProc(m)
End Sub
End Class

这段代码很适合我.

但问题是,如果先连接USB设备,然后再启动应用程序,则无法检测到USB设备.

But the problem is that if the USB device is connected first and the application is started afterwards, then it fails to detect the USB device.

推荐答案

解决方案很简单,只需添加一个计时器双击它输入该代码然后返回到您的启动表单双击它然后输入

That's simple to fix just add a timer double click on it enter that code then go back to your start up form double click on it then type in

   Timer1.start

这将根据您的计时器滴答比每隔几秒检查一次

This will check every so in so seconds depending on your timers tick ratio

10000 = 10 seconds
60000 = 60 seconds
3600000 = 1 hour
7200000 = 2 hours
43200000 = half a day
86400000 = 1 day

但是仍然存在一个问题,消息框会一遍又一遍地出现,并且您会有多层消息框,因此请尝试将计时器中的代码更改为:

However there is still one problem the message box will appear over and over and you'll have layers of message boxes so try changing your code in the timer to this:

Public Class Form1
If label1.text = "0" then 
Private Const WM_DEVICECHANGE As Integer = &H219
Private Const DBT_DEVICEARRIVAL As Integer = 32768

Label1.text = "1"
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_DEVICECHANGE Then
    If m.WParam = DBT_DEVICEARRIVAL Then
        msgbox("Removable Drive Detected", msgboxstyle.OkOnly
        if vbok then label1.text = "0"
    End If
End If
MyBase.WndProc(m)
End Sub
End Class

您需要添加一个标签,如果您不想看到它,只需单击它转到属性(右侧)和

You will need to add a label and if you don't want to see it just click on it go to properties(right side) and

disable visible

这篇关于无法检测到 USB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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