hi ppls
Can somebody tell me some code of how to get hdd number in VB.Net
Many thanks
Printable View
hi ppls
Can somebody tell me some code of how to get hdd number in VB.Net
Many thanks
Not sure exactly what you mean by number, but you should be able to find it using WMI (Windows Management Instrumentation). The 101 examples at the top of the forum have a great example for it (in 2003, not sure about 2005).
You actually need to use the Win32_PhysicalMedia class to get a drive serial number, which is what I assume you mean from that vague description:Note that this will give you the serial number for the physical medium for every drive in your machine, which will be nothing for most of them. You acnnot easily identify which one is which without using the Win32_DiskDrive and Win32_DiskDrivePhysicalMedia classes as well. I have done this myself recently, and I found that the best way was to use the mgmtclassgen.exe utility to generate .NET classes that correspond to the WMI classes and then use code like this:VB Code:
Dim physicalMedia As New Management.ManagementClass("Win32_PhysicalMedia") For Each physicalMedium As Management.ManagementObject In physicalMedia.GetInstances() MessageBox.Show("Serial Number: " & CStr(physicalMedium("SerialNumber"))) Next physicalMediumThe DiskDrivePhysicalMedia, DiskDrive and PhysicalMedia classes are the ones generated by the utility. Do a help search for the name and you'll find a topic that tells you how to use it.VB Code:
Dim driveMedia As New ManagementClass("Win32_DiskDrivePhysicalMedia") Dim driveMedium As DiskDrivePhysicalMedia Dim drive As DiskDrive Dim medium As PhysicalMedia Dim interfaceType As String Dim serialNumber As String 'Look for the first hard disk serial number. For Each mo As ManagementObject In driveMedia.GetInstances() driveMedium = New DiskDrivePhysicalMedia(mo) drive = New DiskDrive(driveMedium.Dependent) medium = New PhysicalMedia(driveMedium.Antecedent) interfaceType = drive.InterfaceType serialNumber = medium.SerialNumber drive.Dispose() medium.Dispose() mo.Dispose() If (interfaceType = "IDE" OrElse interfaceType = "SCSI") AndAlso _ serialNumber <> Nothing Then driveMedia.Dispose() Return serialNumber End If Next mo driveMedia.Dispose()