|
-
Mar 20th, 2000, 05:57 PM
#1
Thread Starter
Junior Member
I need a routine that will simply output the current drives present on a computer. I need the output to display, C:, D: etc.
easy!
[Edited by bazza on 03-21-2000 at 07:14 AM]
-
Mar 20th, 2000, 06:09 PM
#2
Lively Member
there's an API for that...
Declare Function GetLogicalDrives& Lib "kernel32" ()
to use...
dim varNoOfDrives as long
varNoOfDrives = GetLogicalDrives()
-
Mar 21st, 2000, 12:32 PM
#3
Lively Member
here's a way:
Code:
Private Sub Command1_Click()
Dim varNoOfDrives As Long
Dim Counter As Integer
Dim CompareTo
Dim tmpDrvLet As String
Dim lstDrives As String
' *** get the logical Drives
varNoOfDrives = 0
varNoOfDrives = GetLogicalDrives&()
lstDrives = ""
If varNoOfDrives = 0 Then
MsgBox "No Logical Drives Found!", vbCritical, "Error"
Exit Sub
End If
For Counter = 1 To 26
CompareTo = (2 ^ (Counter - 1))
If (varNoOfDrives And CompareTo) <> 0 Then
tmpDrvLet = Chr(Counter + 64) ' Build a drive letter
tmpDrvLet = tmpDrvLet & ":" ' Add the colon
lstDrives = lstDrives & Chr(13) & tmpDrvLet
End If
Next Counter
MsgBox lstDrives
End Sub
don't forget to declare GetLogicalDrives&() just like in my previous post!
[Edited by KenX on 03-22-2000 at 12:34 AM]
-
Mar 21st, 2000, 12:48 PM
#4
or u use this code =)
(past this into a module)
Code:
Private Declare Function GetDriveType Lib "kernel32.dll" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Public Function vbGetDriveType(DrivePath As String) As String
On Error Resume Next
Dim drivetype As Long
drivetype = GetDriveType(DrivePath)
If drivetype = 1 Then vbGetDriveType = ""
If drivetype = 2 Then vbGetDriveType = "REMOVEABLE"
If drivetype = 3 Then vbGetDriveType = "HARD DISK"
If drivetype = 4 Then vbGetDriveType = "NETWORK"
If drivetype = 5 Then vbGetDriveType = "CD-ROM"
If drivetype = 6 Then vbGetDriveType = "RAM-DRIVE": Exit Function
vbGetDriveType = "UNKNOWN TYPE"
End Function
now you can step trought a loop from A till Z ...
if vbGetDriveType returns "" then the drive does not exist.
hope that helps,
taLON
-
Mar 21st, 2000, 04:02 PM
#5
Lively Member
The direct way to solve your problem is use the API
GetLogicalDriveStrings, which does exactly what you
are asking for...I didn't know this API before...
Declare:
Code:
Declare Function GetLogicalDriveStrings Lib "kernel32.dll" Alias
"GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As
Long
Note:
Since each entry in the string takes
four characters (three for the name and one for the null), we can "count by fours" going
through the string until we reach the end. This frees us from worrying about nulls.
Code:
Dim drivenames As String ' receives list of root names
Dim thisdrive As String ' buffer for one extracted root directory name
Dim c As Long ' counter variable
Dim slength As Long ' receives length of returned string
' Make enough room in the buffer to receive the drive names.
drivenames = Space(255) ' more than enough room
' Get the root directory names of all logical drives.
slength = GetLogicalDriveStrings(255, drivenames) ' drivenames now holds the list
' Count by fours to extract the names of each drive.
For c = 1 To slength Step 4 ' loop with an increment of 4
thisdrive = Mid(drivenames, c, 3) ' extract a 3-character string X:\ (X is the drive letter)
Debug.Print thisdrive ' display the drive name
Next c
[Edited by KenX on 03-22-2000 at 04:04 AM]
-
Mar 22nd, 2000, 03:35 AM
#6
Lively Member
I had the same problem a few days ago and here was a solution posted by _bman_ I think.
First add a refereance to Microsoft Scripting Runtime.
Public Sub GetDrives()
Dim fso As New Scripting.FileSystemObject
Dim dr As Scripting.Drive
For Each dr In fso.Drives
Debug.Print dr
Next
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|