Elvio Lujan
asked on
Read the hard disk serial number
Hi.
I need to read the serial number of the hard disk
I need to read the serial number of the hard disk
Private Declare Function GetVolumeInformation _
Lib "kernel32" Alias "GetVolumeInformationA" _
(ByVal lpRootPathName As String, _
ByVal pVolumeNameBuffer As String, _
ByVal nVolumeNameSize As Long, _
lpVolumeSerialNumber As Long, _
lpMaximumComponentLength As Long, _
lpFileSystemFlags As Long, _
ByVal lpFileSystemNameBuffer As String, _
ByVal nFileSystemNameSize As Long) As Long
Public Function GetSerialNumber( _
ByVal sDrive As String) As Long
If Len(sDrive) Then
If InStr(sDrive, "\\") = 1 Then
' Make sure we end in backslash for UNC
If Right$(sDrive, 1) <> "\" Then
sDrive = sDrive & "\"
End If
Else
' If not UNC, take first letter as drive
sDrive = Left$(sDrive, 1) & ":\"
End If
Else
' Else just use current drive
sDrive = vbNullString
End If
' Grab S/N -- Most params can be NULL
Call GetVolumeInformation( _
sDrive, vbNullString, 0, GetSerialNumber, _
ByVal 0&, ByVal 0&, vbNullString, 0)
End Function
'-- Form code
Private Sub Command1_Click()
Dim Drive As String
Drive = InputBox("Enter drive for checking SN")
MsgBox Hex$(GetSerialNumber(Drive ))
End Sub
Lib "kernel32" Alias "GetVolumeInformationA" _
(ByVal lpRootPathName As String, _
ByVal pVolumeNameBuffer As String, _
ByVal nVolumeNameSize As Long, _
lpVolumeSerialNumber As Long, _
lpMaximumComponentLength As Long, _
lpFileSystemFlags As Long, _
ByVal lpFileSystemNameBuffer As String, _
ByVal nFileSystemNameSize As Long) As Long
Public Function GetSerialNumber( _
ByVal sDrive As String) As Long
If Len(sDrive) Then
If InStr(sDrive, "\\") = 1 Then
' Make sure we end in backslash for UNC
If Right$(sDrive, 1) <> "\" Then
sDrive = sDrive & "\"
End If
Else
' If not UNC, take first letter as drive
sDrive = Left$(sDrive, 1) & ":\"
End If
Else
' Else just use current drive
sDrive = vbNullString
End If
' Grab S/N -- Most params can be NULL
Call GetVolumeInformation( _
sDrive, vbNullString, 0, GetSerialNumber, _
ByVal 0&, ByVal 0&, vbNullString, 0)
End Function
'-- Form code
Private Sub Command1_Click()
Dim Drive As String
Drive = InputBox("Enter drive for checking SN")
MsgBox Hex$(GetSerialNumber(Drive
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hello .
You can use this one .
1 - Add System.Management refrence to your project .
2 - Add a Button Control named "Button1" to your project .
'Use the following code...
Class HardDrive
Private model As String = Nothing
Private type As String = Nothing
Private serialNo As String = Nothing
Public Property gModel() As String
Get
Return Model
End Get
Set(ByVal Value As String)
model = Value
End Set
End Property
Public Property gType() As String
Get
Return type
End Get
Set(ByVal Value As String)
type = Value
End Set
End Property
Public Property gSerialNo() As String
Get
Return serialNo
End Get
Set(ByVal Value As String)
serialNo = Value
End Set
End Property
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Result As String
Dim hdCollection As ArrayList = New ArrayList
Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher(" SELECT * FROM Win32_DiskDrive")
For Each wmi_HD As ManagementObject In searcher.Get
Dim hd As HardDrive = New HardDrive
hd.gModel = wmi_HD("Model").ToString
hd.gType = wmi_HD("InterfaceType").To String
hdCollection.Add(hd)
Next
searcher = New ManagementObjectSearcher(" SELECT * FROM Win32_PhysicalMedia")
Dim i As Integer = 0
For Each wmi_HD As ManagementObject In searcher.Get
Dim hd As HardDrive = CType(hdCollection(i), HardDrive)
If wmi_HD("SerialNumber") Is Nothing Then
hd.gSerialNo = "None"
Else
hd.gSerialNo = wmi_HD("SerialNumber").ToS tring
End If
System.Threading.Interlock ed.Increme nt(i)
Next
For Each hd As HardDrive In hdCollection
Result = "Model : " & hd.gModel & vbCrLf
Result &= "Type : " & hd.gType & vbCrLf
Result &= "Serial No : " & hd.gSerialNo
Next
MessageBox.Show(Result)
End Sub
You can use this one .
1 - Add System.Management refrence to your project .
2 - Add a Button Control named "Button1" to your project .
'Use the following code...
Class HardDrive
Private model As String = Nothing
Private type As String = Nothing
Private serialNo As String = Nothing
Public Property gModel() As String
Get
Return Model
End Get
Set(ByVal Value As String)
model = Value
End Set
End Property
Public Property gType() As String
Get
Return type
End Get
Set(ByVal Value As String)
type = Value
End Set
End Property
Public Property gSerialNo() As String
Get
Return serialNo
End Get
Set(ByVal Value As String)
serialNo = Value
End Set
End Property
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Result As String
Dim hdCollection As ArrayList = New ArrayList
Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher("
For Each wmi_HD As ManagementObject In searcher.Get
Dim hd As HardDrive = New HardDrive
hd.gModel = wmi_HD("Model").ToString
hd.gType = wmi_HD("InterfaceType").To
hdCollection.Add(hd)
Next
searcher = New ManagementObjectSearcher("
Dim i As Integer = 0
For Each wmi_HD As ManagementObject In searcher.Get
Dim hd As HardDrive = CType(hdCollection(i), HardDrive)
If wmi_HD("SerialNumber") Is Nothing Then
hd.gSerialNo = "None"
Else
hd.gSerialNo = wmi_HD("SerialNumber").ToS
End If
System.Threading.Interlock
Next
For Each hd As HardDrive In hdCollection
Result = "Model : " & hd.gModel & vbCrLf
Result &= "Type : " & hd.gType & vbCrLf
Result &= "Serial No : " & hd.gSerialNo
Next
MessageBox.Show(Result)
End Sub
I forgot it . Add this line to top of the Public Class FormName :
Imports System.Management
Imports System.Management
http://www.dotnet247.com/247reference/msgs/51/258921.aspx
http://www.vbforums.com/archive/index.php/t-238423.html