| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1247 人关注过本帖
标题:[求助]检测U盘的问题
只看楼主 加入收藏
宝贝学编程
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-8-26
收藏
 问题点数:0 回复次数:3 
[求助]检测U盘的问题
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long

Private Sub Command1_Click()
Dim I As Long, T As Integer
For I = 65 To 90
T = GetDriveType(Chr(I) & ":\")
If T <> 1 Then
Select Case T
Case 2
If I < 67 Then
Print "软驱:" & Chr(I) & "盘"
Else
Print "可移动硬盘:" & Chr(I) & "盘"
End If
Case 3
Print "硬盘:" & Chr(I) & "盘"
Case 4
Print "映射驱动器:" & Chr(I) & "盘"
Case 5
Print "光驱:" & Chr(I) & "盘"
End Select
End If
Next
End Sub

小妹现在想实现在发现U盘驱动器的时候,如何把sys.ini自动复制进去,现在的客户好奇怪,对U盘还产生了兴趣,我在公司打工,哥哥们帮帮忙!
搜索更多相关主题的帖子: U盘 检测 
2007-08-30 14:45
slore
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:1108
专家分:0
注 册:2005-7-1
收藏
得分:0 

'这段代码没有问题呀
Option Explicit

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

Private Sub Command1_Click()
Dim I As Long, T As Integer
For
I = 65 To 90
T = GetDriveType(Chr(I) & ":\")
If T <> 1 Then
Select Case
T
Case 2
If I < 67 Then
Print
"软驱:" & Chr(I) & ""
Else
Print
"可移动硬盘:" & Chr(I) & ""
'这里复制不可以麽?
End If
Case
3
Print "硬盘:" & Chr(I) & ""
Case 4
Print "映射驱动器:" & Chr(I) & ""
Case 5
Print "光驱:" & Chr(I) & ""
End Select
End If
Next
End Sub


快上课了……
2007-08-30 15:56
multiple1902
Rank: 8Rank: 8
等 级:贵宾
威 望:42
帖 子:4881
专家分:671
注 册:2007-2-9
收藏
得分:0 
以下是引用宝贝学编程在2007-8-30 14:45:30的发言:
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long


Private Sub Command1_Click()

Dim I As Long, T As Integer

For I = 65 To 90

T = GetDriveType(Chr(I) & ":\")

If T <> 1 Then

Select Case T

Case 2

If I < 67 Then

Print "软驱:" & Chr(I) & "盘"

Else

Print "可移动硬盘:" & Chr(I) & "盘"

End If

Case 3

Print "硬盘:" & Chr(I) & "盘"

Case 4

Print "映射驱动器:" & Chr(I) & "盘"

Case 5

Print "光驱:" & Chr(I) & "盘"

End Select

End If

Next

End Sub

小妹现在想实现在发现U盘驱动器的时候,如何把sys.ini自动复制进去,现在的客户好奇怪,对U盘还产生了兴趣,我在公司打工,哥哥们帮帮忙!

见此程序
[原创]智能电教辅助系统
http://bbs.bc-cn.net/viewthread.php?tid=166382

2007-08-30 15:59
simpson
Rank: 3Rank: 3
等 级:论坛游民
威 望:7
帖 子:863
专家分:17
注 册:2006-11-16
收藏
得分:0 

Option Explicit
‘子类化窗体消息处理函数时需要使用的API,很常见,不作过多说明。
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd
As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal
lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As
Long, ByVal lParam As Long) As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc
As Any, ByVal ByteLen As Long)
Const GWL_WNDPROC = -4
Const WM_DEVICECHANGE As Long = &H219
Const DBT_DEVICEARRIVAL As Long = &H8000&
Const DBT_DEVICEREMOVECOMPLETE As Long = &H8004&
'设备类型:逻辑卷标
Const DBT_DEVTYP_VOLUME As Long = &H2
'与WM_DEVICECHANGE消息相关联的结构体头部信息
Private Type DEV_BROADCAST_HDR
lSize As Long
lDevicetype As Long '设备类型
lReserved As Long
End Type
'设备为逻辑卷时对应的结构体信息
Private Type DEV_BROADCAST_VOLUME
lSize As Long
lDevicetype As Long
lReserved As Long
lUnitMask As Long '和逻辑卷标对应的掩码
iFlag As Integer
End Type
Public info As DEV_BROADCAST_HDR
Public info_volume As DEV_BROADCAST_VOLUME
Public PrevProc As Long ‘原来的窗体消息处理函数地址
Public Sub HookForm(F As Form)
PrevProc = SetWindowLong(F.hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub UnHookForm(F As Form)
SetWindowLong F.hwnd, GWL_WNDPROC, PrevProc
End Sub
Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam
As Long, ByVal lParam As Long) As Long
Select Case uMsg
'插入USB DISK 则接收到此消息
Case WM_DEVICECHANGE
If wParam = DBT_DEVICEARRIVAL Then

'若插入USBDISK或者映射网络盘等则
'info.lDevicetype =2
'即DBT_DEVTYP_VOLUME

‘利用参数lParam获取结构体头部信息
CopyMemory info, ByVal lParam, Len(info)

If info.lDevicetype = DBT_DEVTYP_VOLUME Then

CopyMemory info_volume, ByVal lParam, Len(info_volume)

'检测到有逻辑卷添加到系统中,则显示该设备根目录下全部文件名
ListFiles Chr(GetDriveName(info_volume.lUnitMask)) & ":\",
Form1.List1
End If
End If

If wParam = DBT_DEVICEREMOVECOMPLETE Then

'若移走USBDISK或者映射网络盘等则
'info.lDevicetype =2
'即DBT_DEVTYP_VOLUME

‘利用参数lParam获取结构体头部信息
CopyMemory info, ByVal lParam, Len(info)

If info.lDevicetype = DBT_DEVTYP_VOLUME Then
CopyMemory info_volume, ByVal lParam, Len(info_volume)

'清除LIST中的内容
Form1.List1.Clear
End If
End If

End Select
' 调用原来的窗体消息处理函数
WindowProc = CallWindowProc(PrevProc, hwnd, uMsg, wParam, lParam)

End Function
'根据输入的32位LONG型数据(只有一位为1)返回对应的卷标的ASCII数值
'规则是1:A、2:B、4:C等等
Function GetDriveName(ByVal lUnitMask As Long) As Byte
Dim i As Long
i = 0
While lUnitMask Mod 2 <> 1
lUnitMask = lUnitMask \ 2
i = i + 1
Wend
GetDriveName = Asc("A") + i
End Function
'显示插入逻辑卷根目录的文件名列表,需要在工程里引用Microsoft Scripting Runtime库。
Function ListFiles(strPath As String, ByRef list As ListBox)
Dim fso As New Scripting.FileSystemObject
Dim objFolder As Folder
Dim objFile As File

Set objFolder = fso.GetFolder(strPath)

For Each objFile In objFolder.Files
list.AddItem objFile.Name
Next
End Function
窗体Form1代码:
Option Explicit
Private Sub Form_Load()
'子类化窗体的消息处理函数
HookForm Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
'程序退出时恢复原窗体处理函数
UnHookForm Me
End Sub
效果图:

备注:本示例程序不仅仅能检测U盘的插入,对CDROM、网络映射盘等设备也会作出同样的反应,如果需要只检测U盘,则需要在If info.lDevicetype =
DBT_DEVTYP_VOLUME
处再对iFlag结构成员作检测,其数值为0时表示设备为U盘。另外根据微软的解释,软盘的插拔是不会有引发该消息的,原因是只有支持软弹出技术的设备才会引发该消息。(原文:Messages
for media arrival and removal are sent only for media in devices that support a
soft-eject mechanism. )
本演示程序在WINDOWS98、XP系统下调试通过。


全国最大的 Java专业电子书免费分享[url]http:///in.asp?id=xrmao[/url]
2007-08-30 17:08
快速回复:[求助]检测U盘的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032534 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved