Results 1 to 15 of 15

Thread: [RESOLVED] [2005]hard disk serial number

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Resolved [RESOLVED] [2005]hard disk serial number

    Hi,

    I am using VB2005

    How can I detect hard disk serial number without API

    Thanks in advance

  2. #2
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [2005]hard disk serial number

    Have a look through System.Management.
    Prefix has no suffix, but suffix has a prefix.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005]hard disk serial number

    You can use WMI through managed code, e.g.
    vb.net Code:
    1. ''' <summary>
    2. ''' Gets a value that identifies this system for licensing purposes.
    3. ''' </summary>
    4. ''' <returns>
    5. ''' An string containing a relatively unique value that identifies the system as being linked to a license key.
    6. ''' </returns>
    7. Private Shared Function GetSystemID() As String
    8.     Dim driveMedium As DiskDrivePhysicalMedia
    9.     Dim drive As DiskDrive
    10.     Dim medium As PhysicalMedia
    11.     Dim interfaceType As String
    12.     Dim serialNumber As String
    13.     Dim systemID As Integer = 0
    14.  
    15.     Using driveMedia As New ManagementClass("Win32_DiskDrivePhysicalMedia")
    16.         For Each mo As ManagementObject In driveMedia.GetInstances()
    17.             driveMedium = New DiskDrivePhysicalMedia(mo)
    18.             drive = New DiskDrive(driveMedium.Dependent)
    19.             medium = New PhysicalMedia(driveMedium.Antecedent)
    20.  
    21.             interfaceType = drive.InterfaceType
    22.             serialNumber = medium.SerialNumber
    23.  
    24.             drive.Dispose()
    25.             medium.Dispose()
    26.             mo.Dispose()
    27.  
    28.             'Look for an IDE or SCSI drive with a serial number.
    29.             If (interfaceType = "IDE" OrElse interfaceType = "SCSI") AndAlso _
    30.                serialNumber <> Nothing Then
    31.                 'Hash the serial number of the first hard drive as the system ID.
    32.                 systemID = serialNumber.GetHashCode()
    33.                 Exit For
    34.             End If
    35.         Next mo
    36.     End Using
    37.  
    38.     If systemID = 0 Then
    39.         'Use the computer name as a fail safe.
    40.         systemID = My.Computer.Name.GetHashCode()
    41.     End If
    42.  
    43.     Return systemID.ToString()
    44. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Re: [2005]hard disk serial number

    Hi,

    I added References to System. Management and Imports System.Management
    Do I have to add anything else ?
    Because I get an errors

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Re: [2005]hard disk serial number

    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?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005]hard disk serial number

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Re: [2005]hard disk serial number

    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

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005]hard disk serial number

    What errors did you get? Are they a secret? If we don't know what they are we can't help you fix them.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Re: [2005]hard disk serial number

    Hi,

    Look at the attached picture.
    Attached Images Attached Images  

  10. #10
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [2005]hard disk serial number

    c# Code:
    1. using System;
    2. using System.Collections;
    3. using System.Management;
    4.  
    5. namespace HardDriveSample1
    6. {
    7.     class HardDrive
    8.     {
    9.         private string model = null;
    10.         private string type = null;
    11.         private string serialNo = null;
    12.  
    13.         public string Model
    14.         {
    15.             get {return model;}
    16.             set {model = value;}
    17.         }
    18.  
    19.         public string Type
    20.         {
    21.             get {return type;}
    22.             set {type = value;}
    23.         }
    24.  
    25.         public string SerialNo
    26.         {
    27.             get {return serialNo;}
    28.             set {serialNo = value;}
    29.         }
    30.     }
    31.  
    32.     class TestProgram
    33.     {
    34.         /// <summary>
    35.         /// The main entry point for the application.
    36.         /// </summary>
    37.         [STAThread]
    38.         static void Main(string[] args)
    39.         {
    40.             ArrayList hdCollection = new ArrayList();
    41.  
    42.             ManagementObjectSearcher searcher = new
    43.                 ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
    44.  
    45.             foreach(ManagementObject wmi_HD in searcher.Get())
    46.             {
    47.                 HardDrive hd = new HardDrive();
    48.                 hd.Model    = wmi_HD["Model"].ToString();
    49.                 hd.Type     = wmi_HD["InterfaceType"].ToString();
    50.  
    51.                 hdCollection.Add(hd);
    52.             }
    53.  
    54.             searcher = new
    55.                 ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
    56.  
    57.             int i = 0;
    58.             foreach(ManagementObject wmi_HD in searcher.Get())
    59.             {
    60.                 // get the hard drive from collection
    61.                 // using index
    62.                 HardDrive hd = (HardDrive)hdCollection[i];
    63.  
    64.                 // get the hardware serial no.
    65.                 if (wmi_HD["SerialNumber"] == null)
    66.                     hd.SerialNo = "None";
    67.                 else
    68.                     hd.SerialNo = wmi_HD["SerialNumber"].ToString();
    69.  
    70.                 ++i;
    71.             }
    72.  
    73.             // Display available hard drives
    74.             foreach(HardDrive hd in hdCollection)
    75.             {
    76.                 Console.WriteLine("Model\t\t: " + hd.Model);
    77.                 Console.WriteLine("Type\t\t: " + hd.Type);
    78.                 Console.WriteLine("Serial No.\t: " + hd.SerialNo);
    79.                 Console.WriteLine();
    80.             }
    81.  
    82.             // Pause application
    83.             Console.WriteLine("Press [Enter] to exit...");
    84.             Console.ReadLine();
    85.         }
    86.     }
    87. }

    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.
    Prefix has no suffix, but suffix has a prefix.

  11. #11
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005]hard disk serial number

    yulyos, if you hover the mouse above those errors, the IDE will give you correction suggestions. You probably havent imported the correct namespaces.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  12. #12
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [2005]hard disk serial number

    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.
    Prefix has no suffix, but suffix has a prefix.

  13. #13
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005]hard disk serial number

    Quote Originally Posted by yulyos
    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
    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

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005]hard disk serial number

    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:
    Quote 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:
    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Aug 2002
    Posts
    224

    Re: [2005]hard disk serial number

    Hi,

    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
    Thanks to everybody

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width