怎么用VB获取本机所有的盘符?并且获取盘符的类别,比如是硬盘、软盘、光驱、或U盘?

解决方案 »

  1.   

    Option Explicit注释:申明API函数
    Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As LongPrivate Sub Form_Load()注释:声明变量
    Dim DrvNum As Single
    Dim drvName As String
    Dim DrvType As Integer
    Dim i As IntegerMe.AutoRedraw = True
    Drive1.Visible = False注释:获取所有常规磁盘信息——
    DrvNum = Asc("a") - 1
    For i = 0 To Drive1.ListCount
    DrvNum = DrvNum + 1
    drvName = Chr(DrvNum) + ":\"
    DrvType = GetDriveType(drvName)
    Select Case GetDriveType(drvName)
    Case 0
    Me.Print "不明 " + "盘符为: " + Trim(UCase(Chr(DrvNum)))
    Case 2
    Me.Print "软驱 " + "盘符为: " + Trim(UCase(Chr(DrvNum)))
    Case 3
    Me.Print "硬盘 " + "盘符为: " + Trim(UCase(Chr(DrvNum)))
    Case 4
    Me.Print "网络盘 " + "盘符为: " + Trim(UCase(Chr(DrvNum)))
    Case 5
    Me.Print "光驱 " + "盘符为: " + Trim(UCase(Chr(DrvNum)))
    Case 6
    Me.Print "RamDisk " + "盘符为: " + Trim(UCase(Chr(DrvNum)))
    End Select
    Next iEnd Sub 
      

  2.   

    为1时是什么“No root directory”偶也搞不懂
      

  3.   

    Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
    Private Sub Form_Load()
        Dim strSave As String
        Dim drvName 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
            drvName = Left$(strSave, InStr(1, strSave, Chr$(0)) - 1)
            Select Case GetDriveType(drvName)
            Case 0
                Me.Print "不明 " + "盘符为: " + drvName
            Case 2
                Me.Print "软驱 " + "盘符为: " + drvName
            Case 3
                Me.Print "硬盘 " + "盘符为: " + drvName
            Case 4
                Me.Print "网络盘 " + "盘符为: " + drvName
            Case 5
                Me.Print "光驱 " + "盘符为: " + drvName
            Case 6
                Me.Print "RamDisk " + "盘符为: " + drvName
            End Select
            strSave = Right$(strSave, Len(strSave) - InStr(1, strSave, Chr$(0)))
        Next keer
    End Sub