December 14, 2007
@ 12:44 PM

.NET still does not expose the drive serial number via the framework. For that you will need to use WMI. Here is the code.

Shared Function GetDriveSerialNumber(ByVal drive As String) As String

    Dim driveSerial As String = String.Empty

 

    'No matter what is sent in, get just the drive letter

    Dim driveFixed As String = System.IO.Path.GetPathRoot(drive)

    driveFixed = Replace(driveFixed, "\", String.Empty)

 

    'Perform Query

    Using querySearch As New ManagementObjectSearcher("SELECT VolumeSerialNumber FROM Win32_LogicalDisk Where Name = '" & driveFixed & "'")

      Using queryCollection As ManagementObjectCollection = querySearch.Get()

        Dim moItem As ManagementObject

        For Each moItem In queryCollection

          driveSerial = CStr(moItem.Item("VolumeSerialNumber"))

          Exit For

        Next

      End Using

    End Using

 

    Return driveSerial

 

End Function

You will also need to reference System.Management in your project.

Tips By: David McCarter

This code and more like it can be found in the dotNetTips.Utility assembly on the CodePlex site: http://www.codeplex.com/dotNetTipsUtility