Reading Serial Number of Drives in VB.net

To index files on removable drives one variable you need, to uniquely identify every drive is the drive’s serial number. Below is a simple method in VB.net to read the serial number of a drive
To start with
1.    Add System.Management as a reference to the project
2.    Also Imports System.Management to the class that will read the serial number
3.    Below is a function to return the drive’s serial Number

Function readDriveSerial (Byval drive as String) as String
‘u have to test if the drive is ready
Dim dsk1 As New ManagementObject("Win32_LogicalDisk.DeviceId=’"     + drive + "’")
dsk1.Get()
Dim serial as String = CType(dsk1("VolumeSerialNumber"), String)
Return serial
End Function

Advertisements

One thought on “Reading Serial Number of Drives in VB.net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s