Results 1 to 3 of 3

Thread: Get HDD Number

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2005
    Location
    Macedonia
    Posts
    49

    Get HDD Number

    hi ppls
    Can somebody tell me some code of how to get hdd number in VB.Net

    Many thanks

  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Get HDD Number

    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).

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

    Re: Get HDD Number

    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:
    VB Code:
    1. Dim physicalMedia As New Management.ManagementClass("Win32_PhysicalMedia")
    2.  
    3.         For Each physicalMedium As Management.ManagementObject In physicalMedia.GetInstances()
    4.             MessageBox.Show("Serial Number: " & CStr(physicalMedium("SerialNumber")))
    5.         Next physicalMedium
    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:
    1. Dim driveMedia As New ManagementClass("Win32_DiskDrivePhysicalMedia")
    2.             Dim driveMedium As DiskDrivePhysicalMedia
    3.             Dim drive As DiskDrive
    4.             Dim medium As PhysicalMedia
    5.             Dim interfaceType As String
    6.             Dim serialNumber As String
    7.  
    8.             'Look for the first hard disk serial number.
    9.             For Each mo As ManagementObject In driveMedia.GetInstances()
    10.                 driveMedium = New DiskDrivePhysicalMedia(mo)
    11.                 drive = New DiskDrive(driveMedium.Dependent)
    12.                 medium = New PhysicalMedia(driveMedium.Antecedent)
    13.  
    14.                 interfaceType = drive.InterfaceType
    15.                 serialNumber = medium.SerialNumber
    16.  
    17.                 drive.Dispose()
    18.                 medium.Dispose()
    19.                 mo.Dispose()
    20.  
    21.                 If (interfaceType = "IDE" OrElse interfaceType = "SCSI") AndAlso _
    22.                    serialNumber <> Nothing Then
    23.                     driveMedia.Dispose()
    24.                     Return serialNumber
    25.                 End If
    26.             Next mo
    27.  
    28.             driveMedia.Dispose()
    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.
    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

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