1,105,197 Community Members

getting the hard drive letter with VB 4

Member Avatar
major_lost
Light Poster
28 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Is there a way to get the hard drive letter with VB 4.0?
I have an older program written in this language that I am attempting to modify
and need to get the hard drive letter to update it.

Thanks for your help.

major_lost

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
1
 

Not sure in vb4. In vb6 you will be using something similar to -

'Module
Public Const Drive_Unknown = 0
Public Const Drive_NoRootDir = 1
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

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

'Code
Dim A As Long
Dim ABC As String
Dim Temp As Long
Dim Desc As String

ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Combo1.Clear
For A = 1 To Len(ABC)
    Temp = GetDriveType(Mid(ABC, A, 1) & ":\")

    Desc = ""
    Select Case Temp
    Case Drive_Unknown
        Desc = "Unknown"
    Case Drive_NoRootDir
        'Desc = "No root directory"
    Case Drive_CDRom
        Desc = "CDRom"
    Case Drive_Fixed
        Desc = "HD"
    Case Drive_RamDisk
        Desc = "Ram drive"
    Case Drive_Remote
        Desc = "Remote"
    Case Drive_Removable
        Desc = "Removable disc"
    End Select

    If Not Desc Like "" Then: Combo1.AddItem Mid(ABC, A, 1) & " - " & Desc
Next
Member Avatar
major_lost
Light Poster
28 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you Andre. The code worked GREAT without any changes. Before I close this thread, can you tell me why it only returns the hard drive versus all of the drive letters? I noticed that 'ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and that the counter cycles through all of the drive letters. I get: "C - Unknown" when I run the code.

Thanks again!!!!!!!!!
major_lost

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

Only a pleasure. I've tested the code on my side and it returned all the correct descriptions...

C-HD
D-HD
E-CDRom
even my removable disk - I- Removable disc
etc...

I tested on Windows 7 and Vista. Not sure if XP will return the correct values.

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

Please mark as solved, thanx.

Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
0
 

Here is another option using the FileSystemObject

Sub ListDrives()
    Dim fso As Scripting.FileSystemObject 'needs refernce to: Microsoft Scripting Runtime
    Dim fsoDrives As Scripting.drives

    Set fso = New FileSystemObject
    Set fsoDrives = fso.drives
    Dim i As Integer
    Dim letter As String
    Dim driveType As String
    For i = Asc("A") To Asc("Z")
        letter = Chr$(i)
        If fso.DriveExists(letter) Then
            Select Case fso.drives(letter).driveType 'returns DriveTypeConst
                Case Scripting.UnknownType
                    driveType = "Unknown"
                Case Scripting.Removable
                    driveType = "Removable"
                Case Scripting.Fixed
                    driveType = "Fixed"
                Case Scripting.Remote
                    driveType = "Remote"
                Case Scripting.CDRom
                    driveType = "CDRom"
                Case Scripting.RamDisk
                    driveType = "RamDisk"
            End Select
            MsgBox "Drive " & letter & " - " & driveType
        End If
    Next i

    Set fsoDrives = Nothing
    Set fso = Nothing

End Sub
Question Answered as of 1 Year Ago by AndreRet and TnTinMN
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article