Had a quick requirement today to retreive a list of drives available to a machine and their respective drive types, e.g. USB, CDROM, Logical Drive, etc. I naturally looked for a .NET solution and could quite easily retrieve the drive letters using System.IO.Directory.GetLogicalDrives property. However, to obtain the DriveType we must goto the System.Management namespace running a 'win32_logicaldisk' query that had to be executed for each individual drive.
However, the time it takes was quite unusable. Now I've written a similar script using the FileSystemObject in VB6 a couple of years. So in the words of Harry Hill..."System.Management namespace or the FileSystemObject to obtain DriveType - well, there's only one way to find out....FIGHT!".
System.Management Namespace (Added System.Management namespace as a Reference).
Dim StartTime As DateTime = Now
Dim Drives As String() = System.IO.Directory.GetLogicalDrives
Dim Drive As String
Dim Disk As System.Management.ManagementObject
Dim DriveType As String
For Each Drive In Drives
Drive = Replace(Drive, "\", "")
Disk = New System.Management.ManagementObject("win32_logicaldisk.deviceid=""" & Drive & """")
Disk.Get()
DriveType = Disk("DriveType").ToString
Debug.WriteLine(Drive & " " & DriveType)
Next
Dim TimeTaken As TimeSpan = Now.Subtract(StartTime)
Debug.WriteLine("System.Management: " & TimeTaken.TotalMilliseconds)
FileSystemObject (Added Microsoft Scripting Runtime as a Reference)
Dim StartTime As DateTime = Now
Dim fso As New Scripting.FileSystemObject
Dim Drives As Scripting.Drives = fso.Drives
Dim Drive As Scripting.Drive
For Each Drive In Drives
Debug.WriteLine(Drive.DriveLetter() & " " & Drive.DriveType)
Next
Dim TimeTaken As TimeSpan = Now.Subtract(StartTime)
Debug.WriteLine("FileSystemObject: " & TimeTaken.TotalMilliseconds)
Now the results were quite surprising. The System.Management namespace averaged out at about 9 seconds, whereas the FSO sped in at an average below one second! Of course, this isn't to say that the System.Management namespace is a load of pants - but in this case the Old Dog Dun Good.