-
Getting Drive Names
If I:
1. hit Ctrl + E,
2. then right click a drive in the "Devices with Removable Storage" and select properties.
3. click the hardware tab.
A very detailed name for each of the disk drives is listed.
Is there a way to get access to these displayed drive names?
-
Re: Getting Drive Names
-
Re: Getting Drive Names
Thanks for the example RhinoBull, although it doesn't get me access to the drive name. Here is another example which gets me the drive label, although again it does not return the drive name:
Code:
Private Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long
Function Drivelabel(ByVal sDrive As String) As String
Const clMaxLen As Long = 255
Dim SDriveLabel As String * clMaxLen, sFileSystem As String * clMaxLen, lSerial As Long
sDrive = Left$(sDrive, 1) & ":\"
Call GetVolumeInformation(sDrive, SDriveLabel, clMaxLen, lSerial, 0, 0, sFileSystem, clMaxLen)
Drivelabel = "Drive label: " & Left$(SDriveLabel, InStr(1, SDriveLabel, vbNullChar) - 1)
End Function
So you see there is a difference between the drive name and the drive label, where the drive name is set by the manufacturer while the drive label is usually set by the user. I am trying to get access to the manufacturer's information regarding the drive including the model, etc.
-
Re: Getting Drive Names
In the absence of an API to get this information, I had to get it via the WMI.
Code:
Option Explicit
' set a reference to the scripting runtime via Project->References->Microsoft Scripting Runtime
' To a form with a command button
' Inspired by http://vbnet.mvps.org/index.html?code/wmi/win32_diskdrive.htm
Private Sub Command1_Click()
MsgBox wmiDiskDriveInfo
End Sub
Code:
Private Function wmiDiskDriveInfo() As String
Dim DiscDrives As Object
Dim DiscDrive As Object
Set DiscDrives = GetObject("winmgmts:{impersonationLevel=impersonate}"). _
InstancesOf("Win32_DiskDrive")
'for each drive return relevant data
For Each DiscDrive In DiscDrives
wmiDiskDriveInfo = wmiDiskDriveInfo & "Model: " & DiscDrive.Model & vbCrLf
wmiDiskDriveInfo = wmiDiskDriveInfo & "Media Type: " & DiscDrive.MediaType & vbCrLf
wmiDiskDriveInfo = wmiDiskDriveInfo & "Interface: " & DiscDrive.InterfaceType & vbCrLf
wmiDiskDriveInfo = wmiDiskDriveInfo & "GB: " & FormatNumber(DiscDrive.Size / 1000000000, 2) & vbCrLf & vbCrLf
Next DiscDrive
Set DiscDrives = Nothing
Set DiscDrive = Nothing
End Function