Hi,
I am using VB2005
How can I detect hard disk serial number without API
Thanks in advance
Printable View
Hi,
I am using VB2005
How can I detect hard disk serial number without API
Thanks in advance
Have a look through System.Management.
You can use WMI through managed code, e.g.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.vb.net Code:
''' <summary> ''' 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
Hi,
I added References to System. Management and Imports System.Management
Do I have to add anything else ?
Because I get an errors
Hi,
I found at: http://www.vbforums.com/showpost.php...50&postcount=3
Little example that posted by jmcilhinney and it's working ok.
My question is if it's doing the same like the code above?
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.
Hi,
First of all thanks jmcilhinney
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
What errors did you get? Are they a secret? If we don't know what they are we can't help you fix them.
Hi,
Look at the attached picture.
c# Code:
using System; using System.Collections; using System.Management; namespace HardDriveSample1 { class HardDrive { private string model = null; private string type = null; private string serialNo = null; public string Model { get {return model;} set {model = value;} } public string Type { get {return type;} set {type = value;} } public string SerialNo { get {return serialNo;} set {serialNo = value;} } } class TestProgram { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { ArrayList hdCollection = new ArrayList(); ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive"); foreach(ManagementObject wmi_HD in searcher.Get()) { HardDrive hd = new HardDrive(); hd.Model = wmi_HD["Model"].ToString(); hd.Type = wmi_HD["InterfaceType"].ToString(); hdCollection.Add(hd); } searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia"); int i = 0; foreach(ManagementObject wmi_HD in searcher.Get()) { // get the hard drive from collection // using index HardDrive hd = (HardDrive)hdCollection[i]; // get the hardware serial no. if (wmi_HD["SerialNumber"] == null) hd.SerialNo = "None"; else hd.SerialNo = wmi_HD["SerialNumber"].ToString(); ++i; } // Display available hard drives foreach(HardDrive hd in hdCollection) { Console.WriteLine("Model\t\t: " + hd.Model); Console.WriteLine("Type\t\t: " + hd.Type); Console.WriteLine("Serial No.\t: " + hd.SerialNo); Console.WriteLine(); } // Pause application Console.WriteLine("Press [Enter] to exit..."); Console.ReadLine(); } } }
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.
yulyos, if you hover the mouse above those errors, the IDE will give you correction suggestions. You probably havent imported the correct namespaces.
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.
Hi yulyos,Quote:
Originally Posted by yulyos
Here's a way to find the serialnumber of your Harddisk.
Use a multiline Textbox to see your HDDserialnumber.
Wkr,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
sparrow1
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:I provided essentially the same advice in that other post you found:Quote:
Originally Posted by jmcilhinney
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.Quote:
Originally Posted by jmcilhinney
Hi,
This little code that posted by jmcilhinney doing all the work.
Thanks to everybodyCode: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