Vb.net 在vb中打开项目时检测Usb
我有一个问题,在代码vb。。。我已将文件传递代码放置到Usb。当您启动程序并插入usb时,它运行正常!但是如果我在打开程序之前放置了usb,那么你必须再次起飞阅读!如何在我打开程序时自动阅读;代码如下: PS:另一个问题:我可以创建一个在所有表单上运行的文件吗?我将命令与文件一起放入表单中;像phpVb.net 在vb中打开项目时检测Usb,vb.net,Vb.net,我有一个问题,在代码vb。。。我已将文件传递代码放置到Usb。当您启动程序并插入usb时,它运行正常!但是如果我在打开程序之前放置了usb,那么你必须再次起飞阅读!如何在我打开程序时自动阅读;代码如下: PS:另一个问题:我可以创建一个在所有表单上运行的文件吗?我将命令与文件一起放入表单中;像php Imports System.Runtime.InteropServices Imports System.IO Public Class projectname Private Cons
Imports System.Runtime.InteropServices
Imports System.IO
Public Class projectname
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 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, "pass.txt")) Then
Using br As New BinaryReader(File.Open(IO.Path.Combine(DriveLetter, "pass.txt"), FileMode.Open))
Try
If br.ReadString = "4553824" Then
Timer_Loading.Start()
Else
MessageBox.Show("Error 1")
End If
Catch ex As EndOfStreamException
End Try
End Using
Else
MessageBox.Show("Error 2")
End If
End If
End If
End If
MyBase.WndProc(m)
End Sub
Private Sub Timer_Loading_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer_Loading.Tick
ProgressBar1.Increment(4)
If ProgressBar1.Value = 100 Then
Dim projectname As New projectname
form2.Show()
Me.Close()
End If
End Sub
End Class
`
您的代码似乎按预期工作:它只查找设备的到达;它不说明设备已经插入,需要添加什么才能工作?关于另一个问题,你知道吗?