SP2 Problem? With "GetDriveType" API Call

joshuaand

Well-known member
Joined
Sep 2, 2003
Messages
45
Location
Australia
Hello,

I am getting a drives type, (Removable Media, HDD, cd rom), and displaying it to the user.

Works great, but not on SP2, it returns a weird drive type (8975933078237085698)

It should return:
"9222812402616107010" = Check for Removable Media
"9222812402616107011" = Check For HardDisks
"9222812402616107013" = Check for Cd-Roms

But as you can see not even close.

Below is the code, anyone know why this is an issue????????



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

        Dim strDrives As String() = Environment.GetLogicalDrives()
        Dim drive As String
        For Each drive In strDrives
            Dim strType As String = GetDriveType(drive)
            Dim driveSTR As String = "Unknown Type : "

            TextBox1.Text &= (drive & " is type : ")

            Select Case strType
                Case "9222812402616107010"
                    Check for Removable Media
                    driveSTR = "Removable Media"
                Case "9222812402616107011"
                    Check For HardDisks
                    driveSTR = "Hard Disc"
                Case "9222812402616107013"
                    Check for Cd-Roms
                    driveSTR = "CD-ROM"
                Case Else
                    driveSTR &= strType
            End Select
            TextBox1.Text &= driveSTR & vbCrLf
        Next
 
You could use the System.Management namespace instead like
Code:
Dim diskClass As New ManagementClass("Win32_LogicalDisk")
        Dim disks As ManagementObjectCollection = diskClass.GetInstances()
        Dim disksEnumerator As _
        ManagementObjectCollection.ManagementObjectEnumerator = _
        disks.GetEnumerator()
        While disksEnumerator.MoveNext()
            Dim disk As ManagementObject = _
            DirectCast(disksEnumerator.Current, ManagementObject)

            Drive letter is the caption, message is the type where
            NoRoot = 1, Removable = 2, LocalDisk = 3, Network = 4,CD = 5, RAMDrive = 6
            MessageBox.Show(disk("DriveType").ToString, disk("deviceid").ToString)
        End While

or alternatively change the declaration to
Code:
Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Integer
 
Thanks!

Thankyou,

I used a base of your code to end up with the following, which gives me everything:

Hello,

Code:
        TextBox1.Text = ""
        Dim diskClass As New ManagementClass("Win32_LogicalDisk")
        Dim disks As ManagementObjectCollection = diskClass.GetInstances()
        Dim disksEnumerator As ManagementObjectCollection.ManagementObjectEnumerator = disks.GetEnumerator()
        While disksEnumerator.MoveNext()
            Dim disk As ManagementObject = DirectCast(disksEnumerator.Current, ManagementObject)

            Dim propNames As StringCollection = New StringCollection
            Dim props As PropertyDataCollection = diskClass.Properties

            For Each driveProperty As PropertyData In props
                propNames.Add(driveProperty.Name)
            Next

            TextBox1.Text &= " Drive " & disk("deviceid").ToString & "\ Properties: " & vbCrLf

            Dim idx As Int16 = 0

            For Each strProp As String In propNames
                If Not disk(strProp) Is Nothing Then
                    TextBox1.Text &= strProp & ": " & disk(strProp).ToString
                Else
                    TextBox1.Text &= strProp & ": NOTHING"
                End If
                TextBox1.Text &= vbCrLf
            Next
            TextBox1.Text &= vbCrLf & vbCrLf

        End While
 
Back
Top