Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Sub Form_Load()
Dim strSave As String
'Set the graphic mode to persistent
Me.AutoRedraw = True
'Create a buffer to store all the drives
strSave = String(255, Chr$(0))
'Get all the drives
ret& = GetLogicalDriveStrings(255, strSave)
'Extract the drives from the buffer and print them on the form
For keer = 1 To 100
If Left$(strSave, InStr(1, strSave, Chr$(0))) = Chr$(0) Then Exit For
Me.Print Left$(strSave, InStr(1, strSave, Chr$(0)) - 1)
strSave = Right$(strSave, Len(strSave) - InStr(1, strSave, Chr$(0)))
Next keer
End Sub
VB声明
Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
说明
获取一个字串,其中包含了当前所有逻辑驱动器的根驱动器路径
返回值
Long,装载到lpBuffer的字符数量(排除空中止字符)。如缓冲区的长度不够,不能容下路径,则返回值就变成要求的缓冲区大小。零表示失败。会设置GetLastError
参数表
参数 类型及说明
nBufferLength Long,lpBuffer字串的长度
lpBuffer String,用于装载逻辑驱动器名称的字串。每个名字都用一个NULL字符分隔,在最后一个名字后面用两个NULL表示中止(空中止)