编程识别U盘

DawnPine 2004-06-24 08:28:02
把U盘插到机器上后,U盘能马上被认出来,正确工作。如果点击系统托盘里的图标将其弹出后,这个设备就象不存在于系统中一样,尽管它仍插在主机的USB口。如果想让它再次正常工作,只能再拨、插一次,或者重启系统,除此之后再无别的办法(在设备管理器里描扫硬件改动也不能凑效) :-(

能不能通过编程的办法让USB设备被认出来?
...全文
213 点赞 收藏 12
写回复
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
DawnPine 2004-09-21
这些解决办法没实现我的目标,看来没什么好办法,只能这样了:(
回复
LanceJin 2004-07-01
可惜我现在转回单片机,连vb都没装,自己没电脑,无法试

up
回复
DawnPine 2004-06-30
我也UP
回复
ryuginka 2004-06-29
up
回复
DawnPine 2004-06-29
金DX,你给的链接我看过了。我要的不是这个东东 :-(

有没有别的办法?
回复
LanceJin 2004-06-28
有人总喜欢将别人联接里的内容拷出来..........
罢了,罢了
回复
ywq126 2004-06-28
关注一下 mark
回复
DawnPine 2004-06-28
感谢楼上2位DX的帮助
只是给出的链接及源码与我本意有些差异

我要的不是在系统中已分配了盘符的驱动器及其相关信息
而是要系统主动地去识别一个已存在的USB设备(如U盘)

不知哪位DX能助一臂之力?
回复
LanceJin 2004-06-25
How to Enumerate Available System Drives
http://vbnet.mvps.org/code/disk/drivetype.htm
回复
LanceJin 2004-06-25
用WMI
http://vbnet.mvps.org/code/wmi/wmilogicaldiskinfo.htm
回复
cso 2004-06-25
Public Declare Function GetLogicalDriveStrings Lib "kernel32" _
Alias "GetLogicalDriveStringsA" _
(ByVal nBufferLength As Long, _
ByVal lpBuffer As String) As Long

Public Declare Function GetDriveType Lib "kernel32" _
Alias "GetDriveTypeA" _
(ByVal nDrive As String) As Long

Public Const DRIVE_REMOVABLE = 2
Public Const DRIVE_FIXED = 3
Public Const DRIVE_REMOTE = 4
Public Const DRIVE_CDROM = 5
Public Const DRIVE_RAMDISK = 6
===========================================================

Option Explicit

Private Sub Form_Load()

Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2

End Sub


Private Sub Command2_Click()

Unload Me

End Sub


Private Sub Command1_Click()

Dim r As Long
Dim allDrives As String
Dim currDrive As String
Dim drvType As String

'get the list of all available drives
allDrives = GetDriveString()

'separate the drive strings and retrieve the drive type
Do Until allDrives = Chr$(0)

'strip off one drive from the string allDrives
currDrive = StripNulls(allDrives)

'get the drive type
drvType = rgbDrvType(currDrive)

Print " " & currDrive & vbTab & drvType

Loop

End Sub


Private Function rgbDrvType(RootPathName) As String

'Passed is the drive to check.
'Returned is the type of drive.
Select Case GetDriveType(RootPathName)
Case 0: rgbDrvType = "The drive type cannot be determined"
Case 1: rgbDrvType = "The root directory does not exist"

Case DRIVE_REMOVABLE:
Select Case Left$(RootPathName, 1)
Case "a", "b": rgbDrvType = "Floppy drive"
Case Else: rgbDrvType = "Removable drive"
End Select

Case DRIVE_FIXED: rgbDrvType = "Hard drive; can not be removed"
Case DRIVE_REMOTE: rgbDrvType = "Remote (network) drive"
Case DRIVE_CDROM: rgbDrvType = "CD-ROM drive"
Case DRIVE_RAMDISK: rgbDrvType = "RAM disk"
End Select

End Function


Private Function GetDriveString() As String

'returns string of available
'drives each separated by a null
Dim sBuffer As String

'possible 26 drives, three characters each, plus trailing null
sBuffer = Space$(26 * 4)

If GetLogicalDriveStrings(Len(sBuffer), sBuffer) Then

'do not trim off trailing null!
GetDriveString = Trim$(sBuffer)

End If

End Function



Private Function StripNulls(startstr As String) As String

'Take a string separated by chr$(0)
'and split off 1 item, shortening the
'string so next item is ready for removal.
Dim pos As Long

pos = InStr(startstr$, Chr$(0))

If pos Then

StripNulls = Mid$(startstr, 1, pos - 1)
startstr = Mid$(startstr, pos + 1, Len(startstr))

End If

End Function
回复
Chice_wxg 2004-06-24
关注一下 mark
回复
发动态
发帖子
API
创建于2007-09-28

1455

社区成员

VB API
申请成为版主
社区公告
暂无公告