VB.NET – 取得 CPU、硬碟、主機板 序號/SN

有些人在寫程式的時候…

會想說程式該怎麼寫…

才有辦法查詢該電腦的CPU、硬碟、主機板等等..的序號

這邊就教你該怎麼寫…

此程式碼非原創(來源也忘了在哪找的),最近整理資料,所以順便分享出來..

假若原創作者有看到,記得通知我一下…

其實抓取這些設備的SN…有很多用途….

像是寫完的程式…

如果只想給某些特定人士、付費者使用…

就可以使用此方式…畢竟這些設備不常更換….

    '取得CPU序號
    Public Declare Sub GetSystemInfo Lib "kernel32" (ByRef lpSystemInfo As SYSTEM_INFO)
    Public Structure SYSTEM_INFO
        Dim dwOemID As Integer
        Dim wProcessorArchitecture As Integer
        Dim wReserved As Integer
        Dim dwPageSize As Integer
        Dim lpMinimumApplicationAddress As Integer
        Dim lpMaximumApplicationAddress As Integer
        Dim dwActiveProcessorMask As Integer
        Dim dwNumberOrfProcessors As Integer
        Dim dwProcessorType As Integer
        Dim dwAllocationGranularity As Short
        Dim wProcessLevel As Short
        Dim wProcessorRevision As Short
    End Structure
    Public Function GetCPUCode() As String
        Dim CPUInfo As SYSTEM_INFO
        GetSystemInfo(CPUInfo)
        Return CPUInfo.dwProcessorType
    End Function
    '取得硬碟序號
    Private Function GetHardDriveCode(ByVal DrvIdx As Byte) As String
        Dim WMI As Object = GetObject("winmgmts:")
        Dim strCls As String = "Win32_PhysicalMedia"
        Dim strKey As String = strCls & ".Tag=""\\\\.\\PHYSICALDRIVE" & DrvIdx & """"
        Return WMI.InstancesOf(strCls)(strKey).SerialNumber.ToString.Trim
        System.Runtime.InteropServices.Marshal.ReleaseComObject(WMI)
    End Function
    '取得主機板序號
    Private Function GetMotherBoardCode() As String
        Dim WMI As Object = GetObject("winmgmts:")
        Dim strCls As String = "Win32_BaseBoard"
        Dim strKey As String = strCls & ".Tag=""Base Board"""
        Return WMI.InstancesOf(strCls)(strKey).SerialNumber.ToString.Trim
        System.Runtime.InteropServices.Marshal.ReleaseComObject(WMI)
    End Function

1 個留言

  1. RC 說:

    硬碟序號不會顯示出來!

    版主回覆:
    我想應該是此程式碼無法套用到全部電腦…
    因為我有在兩台不同電腦測試過…
    一台不能顯示主機板序號,另一台是主機板、硬碟都無法顯示….
    此程式碼也非本人原創..取之網路…
    剛好最近整理…就順便分享出來…

發表留言 »

姓名
信箱
網站