Get Hard Disk Serial and CPUID in .Net

When we sell a software we may decide to make it work only on a specific computer generating a license key that works only for that PC. When the customer changes his hardware he has to ask for a new license key. We can identify a computer retrieving it’s CPU ID and it’s primary disk serial number. In .Net we can do this using the win32 APIs or WMI.
WMI stands “Windows Management Instrumentation”. WMI is a new management technology allowing scripts to monitor and control managed resources throughout the network. Resources include hard drives, file systems, operating system settings, processes, services, shares, registry settings, networking components, event logs, users, and groups. WMI is built into clients with Windows 2000 or above, and can be installed on any other 32-bit Windows client. (description from http://www.rlmueller.net/terms.htm)

Here is the code to query the infos we need:

Public Function GetHDSerial() As String
   Dim disk As New ManagementObject(_
       "Win32_LogicalDisk.DeviceID=""C:""")
   Dim diskPropertyA As PropertyData = _
       disk.Properties("VolumeSerialNumber")
   Return diskPropertyA.Value.ToString()
End Function
 
Public Function GetCPUId() As String
   Dim cpuInfo As String = String.Empty
   Dim temp As String = String.Empty
   Dim mc As ManagementClass = _
       New ManagementClass("Win32_Processor")
   Dim moc As ManagementObjectCollection =_
       mc.GetInstances()
   For Each mo As ManagementObject In moc
     If cpuInfo = String.Empty Then
        cpuInfo = _
         mo.Properties("ProcessorId").Value.ToString()
     End If
   Next
   Return cpuInfo
End Function

Download this code: hdserialandcpuid.vb.txt

there are a lot of ways to query devices info thowards WMI but it’s really difficult to find a good and complete documentation but i found on the web a good tool to explore wmi (WMI Explorer), you can download it here http://www.ks-soft.net/hostmon.eng/wmi/index.htm


TAGS: Asp.net, dotnet, code snippet, seo, search engine optimization, visual studio.net, sample code, c#, vb.net

7 Comments so far »

  1. quangviet said

    am June 22 2007 @ 11:01 am

    please help me,way read serial DiskDrive use VB.net,not serial Disk C or D..

  2. Mohammadreza said

    am July 18 2007 @ 5:02 pm

    Your information was really useful.
    Thank you very much!!

  3. Haris PP said

    am October 6 2007 @ 6:33 am

    This Codes are very much helpful to get Hard disc id and CPU id thank you

  4. Samant said

    am October 15 2007 @ 11:45 am

    When i try to Get my CPU info its throw Null Exception.
    How to get my CPU info using another way in VB.net. I am using Windows XP

  5. GW said

    am November 22 2007 @ 5:29 am

    I get error with this code

    ManagementClass is not defined

  6. rajeev said

    am January 2 2008 @ 4:02 pm

    thanks.. it works.. you got to add reference to system.management class in your project..

  7. ashish said

    am January 5 2008 @ 3:50 pm

    Got Error
    Management object not defined

Comment RSS · TrackBack URI

Leave a comment

Name:

eMail:

Website:

Comment: