Wednesday, December 14, 2005

Drive Type

I am currently working on a project that imports data from a SD card and I wanted an easy way to find the drive letter that windows creates for the device so that it was defaulted for the user. After a bit of searching I found that I could use WMI to do this and it is quite easy. Here is my implementation:

Public Enum DiskDriveType
Unkown = 0
NotRootDirectory = 1
RemovableDisk = 2
LocalDisk = 3
NetworkDrive = 4
CompactDisk = 5
RamDisk = 6
End Enum

Public Function DriveType(ByVal driveLetter As String) As DiskDriveType
Dim sms As New System.Management.ManagementObject(String.Format("Win32_logicaldisk='{0}:'", DriveLetter))
Select Case Convert.ToInt32(sms.Properties("DriveType").Value)
Case 0 : Return DiskDriveType.Unkown
Case 1 : Return DiskDriveType.NotRootDirectory
Case 2 : Return DiskDriveType.RemovableDisk
Case 3 : Return DiskDriveType.LocalDisk
Case 4 : Return DiskDriveType.NetworkDrive
Case 5 : Return DiskDriveType.CompactDisk
Case 6 : Return DiskDriveType.RamDisk
Case Else : Return DiskDriveType.Unkown
End Select
End Function

One thing to be warry of is that floppy drives are also considered removable devices so I put a hardcode in to skip the default drive letter for a floppy ("A:\"). I am thinking of improving this in the future to check the capacity of the device to avoid this issue (but then i might get confused between SD cards and external hard drives)


For Each driveLetter As String In System.IO.Directory.GetLogicalDrives()
If driveLetter.Substring(0, 1) <> "A" And DriveType(driveLetter.Substring(0, 1)) = DiskDriveType.RemovableDisk Then
txtSDDrive.Text = driveLetter
End If


Post a Comment

<< Home