在vf的下拉控件的combo1,如何读出接入的U盘啊

silandn 2011-06-18 01:45:55
在vf的下拉控件的combo1,如何读出接入的U盘啊、

用VF编个程序,有个下拉框,combo1.如果电脑没有U盘接入的时候下拉框为空。

当有u盘接入的时候,下来框中可以显示这个U盘的盘符及容量。

请问一下,这样有办法么?
...全文
128 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzy1888 2011-08-23
  • 打赏
  • 举报
回复
十豆三,有空加一下我的QQ,有事请教咯.179793466
silandn 2011-06-18
  • 打赏
  • 举报
回复
好了,果然可以了,多谢十豆三的热心帮助。
十豆三 2011-06-18
  • 打赏
  • 举报
回复
已更新4楼代码,带容量了。
silandn 2011-06-18
  • 打赏
  • 举报
回复
哇,可以了多谢十豆三先生。

请问有没有读出容量的办法啊。就是能能在盘符后面显示容量的,如4g。
十豆三 2011-06-18
  • 打赏
  • 举报
回复
VFP9.0,把下面代码放到你的 Combo1 控件的 Init 事件:

This.Clear
m.loWMI = Getobject("winmgmts:\\.\root\cimv2")
m.loDisks = m.loWMI.ExecQuery([Select * From Win32_DiskDrive Where InterfaceType="USB"])
m.loParts = m.loWMI.ExecQuery([Select * From Win32_LogicalDiskToPartition])
For Each m.loDisk In m.loDisks
m.lcDevID = 'Disk #' + Transform(m.loDisk.Index)
For Each m.loPart In m.loParts
If At(m.lcDevID, m.loPart.Antecedent) > 0
This.AddItem(Strextract(m.loPart.Dependent, [DeviceID="], ["], 1)+' 容量:'+Transform(Diskspace(Strextract(m.loPart.Dependent, [DeviceID="], ["], 1),1)/1024/1024/1024)+'G')
Endif
Endfor
Endfor
Release m.loWMI

然后再加个 Timer 控件,
此控件的 Interval 值为1000,意思是1秒执行一行
此控件的 Timer 事件代码为:
This.Enabled= .F.
Thisform.Combo1.Init()
This.Enabled= .T.
silandn 2011-06-18
  • 打赏
  • 举报
回复
如果将代码写在combo1的focus事件中,运行的时候是可以显示盘符,但是显示在表单中,而不是显示在combo1的下拉中,也不是combo1的值,请问该如何写呢
silandn 2011-06-18
  • 打赏
  • 举报
回复
我的是vfp9,请问上面代码写在什么事件中呢,我写在click和init事件中,都不能读出u盘的。
十豆三 2011-06-18
  • 打赏
  • 举报
回复
WIN API WIMI-获取U盘盘符
*-----------------------
* VFP9.0

Clear
m.loWMI = Getobject("winmgmts:\\.\root\cimv2")
m.loDisks = m.loWMI.ExecQuery([Select * From Win32_DiskDrive Where InterfaceType="USB"])
m.loParts = m.loWMI.ExecQuery([Select * From Win32_LogicalDiskToPartition])
For Each m.loDisk In m.loDisks
? 'USB 设备(' + m.loDisk.Caption + ')分配的盘符:'
m.lcDevID = 'Disk #' + Transform(m.loDisk.Index)
For Each m.loPart In m.loParts
If At(m.lcDevID, m.loPart.Antecedent) > 0
?? " " + Strextract(m.loPart.Dependent, [DeviceID="], ["], 1)
Endif
Endfor
Endfor
--------------------

Create Cursor diskinfo1 (DiskName C(30),DiskIndex I,DiskInterfaceType C(50))
oWMI=Getobject( 'winmgmts:' )
oItems=oWMI.ExecQuery('Select * From Win32_DiskDrive Where InterfaceType="USB"')
For Each oItem In oItems
Insert Into diskinfo1 Values (oItem.Caption,oItem.Index,oItem.InterfaceType)
Endfor

Create Cursor diskinfo2 (Antecedent C(200),Dependent C(200))
*oWMI=Getobject( 'winmgmts:' )
oItems=oWMI.ExecQuery('select * from Win32_LogicalDiskToPartition')
For Each oItem In oItems
Insert Into diskinfo2 Values (oItem.Antecedent,oItem.Dependent)
Endfor

Select Substr(A.Dependent,At(["],A.Dependent,1)+1,2) As U盘盘符 From diskinfo2 A Inner Join diskinfo1 B ;
On Val(Substr(A.Antecedent,At([#],A.Antecedent,1)+1,1))=B.DiskIndex


-------------------
* VFP6.0
Create Cursor diskinfo1 (DiskName C(30),DiskIndex I,DiskInterfaceType C(50))
oWMI1=Createobject("WbemScripting.SWbemLocator")
oWMI=oWMI1.ConnectServer(".", "root\cimv2")
oItems=oWMI.ExecQuery('select * from Win32_DiskDrive')
For Each oItem In oItems
Insert Into diskinfo1 Values (oItem.Caption,oItem.Index,oItem.InterfaceType)
Endfor

Create Cursor diskinfo2 (Antecedent C(200),Dependent C(200))
*oWMI=Getobject( 'winmgmts:' )
oItems=oWMI.ExecQuery('select * from Win32_LogicalDiskToPartition')
For Each oItem In oItems
Insert Into diskinfo2 Values (oItem.Antecedent,oItem.Dependent)
Endfor

Select Substr(A.Dependent,At(["],A.Dependent,1)+1,2) As U盘盘符 From diskinfo2 A Inner Join diskinfo1 B ;
On Val(Substr(A.Antecedent,At([#],A.Antecedent,1)+1,1))=B.DiskIndex Where B.DiskInterfaceType="USB"

--------------------
VFP9.0

VB codeCreate Cursor diskinfo (DeviceID C(30),Description C(50),Drivetype I)
oWMI = Getobject( 'winmgmts:' )
oItems = oWMI.ExecQuery( 'select * from Win32_LogicalDisk' )
For Each oItem In oItems
Insert Into diskinfo Values (oItem.DeviceID,oItem.Description,oItem.Drivetype)
Endfor
Locate
Browse

-------------------
#Define MAX_PATH 260
Do Declare
Create Cursor csDosDevices (drvletter C(2),drvtype I,targetpath C(250))
Local cDrives,nBufsize,cDrvLetter,ch,cTargetPath
cDrives = Replicate(Chr(0),250)
nBufsize = GetLogicalDriveStrings(Len(cDrives),@cDrives)
cDrives = Padr(cDrives,nBufsize)
cDrvLetter=""
For nBufsize=1 To Len(cDrives)
ch = Substr(cDrives,nBufsize,1)
If ch = Chr(0)
cDrvLetter=Padr(cDrvLetter,2)
cTargetPath = Replicate(Chr(0),MAX_PATH)
= QueryDosDevice(cDrvLetter,@cTargetPath,Len(cTargetPath))
cTargetPath = Strtran(cTargetPath,Chr(0),"")
Insert Into csDosDevices Values (cDrvLetter,GetDriveType(cDrvLetter),cTargetPath)
cDrvLetter = ""
Else
cDrvLetter = cDrvLetter + m.ch
Endif
Next
Go Top
Browse Normal Nowait
Procedure Declare
Declare Integer GetDriveType In kernel32 String nDrive
Declare Integer GetLogicalDriveStrings In kernel32 Integer nBufferLength,String @lpBuffer
Declare Integer QueryDosDevice In kernel32 String lpDeviceName,String @lpTargetPath,Integer ucchMax
Endproc

-------------------

Public DriName
mydri =' '
Declare Integer GetLogicalDriveStrings In kernel32 Intege nBufferLength,String @ lpBuffer
Declare Integer GetDriveType In kernel32 String nDrive
AIIDrive=Replicate(' ',108)
=GetLogicalDriveStrings(Len(AIIDrive),@AIIDrive)
AIIDrive=Allt(AIIDrive)
For i=1 To Len(AIIDrive)/4
DriNameTmp=Substr(AIIDrive,i*4-3,2)
NDType=GetDriveType(DriNameTmp)
Do Case
Case NDType=2 And (DriNameTmp='A:' Or DriNameTmp='B:' Or DriNameTmp='a:' Or DriNameTmp='b:')
DriType='软驱'
Case NDType=2 And (DriNameTmp<>'A:' And DriNameTmp<>'B:' And DriNameTmp<>'a:' And DriNameTmp<>'b:')
DriType='U盘'
mydri=mydri+DriNameTmp+' '+DriType
Case NDType=3
DriType='硬盘'
Case NDType=5
DriType='光驱'
Endc
Endf
Wait Window mydri

2,726

社区成员

发帖
与我相关
我的任务
社区描述
VFP,是Microsoft公司推出的数据库开发软件,用它来开发数据库,既简单又方便。
社区管理员
  • VFP社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧