''' Gets a value that identifies this system for licensing purposes.
''' </summary>
''' <returns>
''' An string containing a relatively unique value that identifies the system as being linked to a license key.
''' </returns>
Private Shared Function GetSystemID() As String
Dim driveMedium As DiskDrivePhysicalMedia
Dim drive As DiskDrive
Dim medium As PhysicalMedia
Dim interfaceType As String
Dim serialNumber As String
Dim systemID As Integer = 0
Using driveMedia As New ManagementClass("Win32_DiskDrivePhysicalMedia")
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()
'Look for an IDE or SCSI drive with a serial number.
If (interfaceType = "IDE" OrElse interfaceType = "SCSI") AndAlso _
serialNumber <> Nothing Then
'Hash the serial number of the first hard drive as the system ID.
systemID = serialNumber.GetHashCode()
Exit For
End If
Next mo
End Using
If systemID = 0 Then
'Use the computer name as a fail safe.
systemID = My.Computer.Name.GetHashCode()
End If
Return systemID.ToString()
End Function
Note that the DiskDrivePhysicalMedia, DiskDrive and PhysicalMedia classes are all .NET classes generated by the MgmtClassGen.exe utility that comes with the .NET Framework SDK. They each correspond to an unmanaged WMI class. You can find information on using the utitlity in the MSDN library, then just import the generated class files into your project.
If you need to ask that question then you haven't really looked at the code, or read what was posted, very carefully. In both posts I use the same class names and the same variable names, and I mentioned using the same utility to generate the .NET wrappers for the WMI classes in both. This thread's code has just been changed a little because it's in a different app and it's been updated slightly for VB 2005. Other than that they are almost identical.
It's weird. I found the above c# code that works but when I converted it to vb.net it came up with some errors. Mainly it said that System.Management contained no public members. I do not know the solution.
Actually Atheist, I pasted the same code and the errors do not show any suggestions. I also tried searching for the underlined words in the object browser and it came up empty.
The code that you post in this thread I get errors. And I wrote it.
Because of the errors I search for other code.
Thanks again
Hi yulyos,
Here's a way to find the serialnumber of your Harddisk.
Use a multiline Textbox to see your HDDserialnumber.
Code:
Imports System
Imports System.Collections
Imports System.Management
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim hdCollection As New ArrayList()
Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive")
Dim wmi_HD As New ManagementObject()
For Each wmi_HD In searcher.Get
Dim hd As New HardDrive()
hd.Model = wmi_HD("Model").ToString()
hd.Type = wmi_HD("InterfaceType").ToString()
hdCollection.Add(hd)
Next
Dim searcher1 As New ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia")
Dim i As Integer = 0
For Each wmi_HD In searcher1.Get()
'// get the hard drive from collection
'// using index
Dim hd As HardDrive
hd = hdCollection(i)
'// get the hardware serial no.
If wmi_HD("SerialNumber") = "" Then
hd.serialNo = "None"
Else
hd.serialNo = wmi_HD("SerialNumber").ToString()
i += 1
End If
Next
Dim hd1 As HardDrive
Dim ii As Integer = 0
For Each hd1 In hdCollection
ii += 1
TextBox1.Text = TextBox1.Text + "Disco #: " + ii.ToString + Chr(13) + Chr(10)
TextBox1.Text = TextBox1.Text + "Model: " + hd1.Model + Chr(13) + Chr(10)
TextBox1.Text = TextBox1.Text + "Tipo: " + hd1.Type + Chr(13) + Chr(10)
TextBox1.Text = TextBox1.Text + "Serial No: " + hd1.serialNo + Chr(13) + Chr(10) + Chr(13) + Chr(10)
Next
End Sub
End Class
Public Class HardDrive
Private dsk_model As String
Private dsk_type As String
Private dsk_serialNo As String
Public Property Model() As String
Get
Return dsk_model
End Get
Set(ByVal value As String)
dsk_model = value
End Set
End Property
Public Property Type() As String
Get
Return dsk_type
End Get
Set(ByVal value As String)
dsk_type = value
End Set
End Property
Public Property serialNo() As String
Get
Return dsk_serialNo
End Get
Set(ByVal value As String)
dsk_serialNo = value
End Set
End Property
End Class
Wkr,
sparrow1
Wkr,
sparrow1
If I helped you, don't forget to Rate my post. Thank you
It is my feeling that if you post code then people copy the code and ignore anything else you post. That's why I avoid posting code a lot of the time: because I want people to read my words, think about them and develop some understanding of what I'm trying to convey. It seems that this is another case of code copied and words ignored. No doubt the error messages say that those classes are not defined. In the very same post that you copied that code from I told you how to define those classes:
Originally Posted by jmcilhinney
Note that the DiskDrivePhysicalMedia, DiskDrive and PhysicalMedia classes are all .NET classes generated by the MgmtClassGen.exe utility that comes with the .NET Framework SDK. They each correspond to an unmanaged WMI class. You can find information on using the utitlity in the MSDN library, then just import the generated class files into your project.
I provided essentially the same advice in that other post you found:
Originally Posted by jmcilhinney
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: ... The 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.
If you're going to just copy and paste code without reading the explanation that goes with then you're only going to get half the story.
This little code that posted by jmcilhinney doing all the work.
Code:
Imports System.Management
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 physicalMedium