C#/VB.NET 擷取電腦屬性(硬碟ID、硬碟容量、Cpu序號、MAC地址、系統類別型)

來源:互聯網
上載者:User

標籤:sea   圖片   cal   str   height   led   ber   unit   1.5   

原文:C#/VB.NET 擷取電腦屬性(硬碟ID、硬碟容量、Cpu序號、MAC地址、系統類別型)

在開發過程中,經常需要擷取電腦的一些屬性,如擷取硬碟ID/CPU序號/MAC地址作為來加密字串。

1、硬碟

在我查看網上一些文檔時,發現很多人對硬碟序號很模糊~

什麼叫硬碟序號?指的是作為一個硬碟的標識;但是有時候發現,同事的電腦硬碟序號有重複的。所以硬碟標識我們可以試試用ID,這個如何擷取下面講解。

我們可以運行DOS命令,或者查看註冊表,查看硬碟的序號。中所示:都是HGST HTS725050A7E630。

大部分情況下,大家都可以通過如下擷取:

        Dim key As RegistryKey = Registry.LocalMachine        Dim subKey = key.OpenSubKey("HARDWARE\DEVICEMAP\Scsi\Scsi Port 0\Scsi Bus 0\Target Id 0\Logical Unit Id 0")        Dim bbb = subKey.GetValue("Identifier")

也可以通過Wmi方式擷取:

        Dim cmicWmi As New System.Management.ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive")        Dim diskId As String ‘數字ID        Dim diskSerialNumber As String ‘這個我們暫且稱其為序號碼        Dim diskModel As String ‘序號        For Each cmicWmiObj As ManagementObject In cmicWmi.Get            diskId = cmicWmiObj("signature")            diskSerialNumber = cmicWmiObj("serialnumber")            diskModel = cmicWmiObj("Model")        Next

個人建議用Wmiy中的signature來作為ID,畢竟是數字,且不重複。

PS:順帶提一個硬碟的容量,通過屬性size可以擷取單個硬碟的大小,加起來的話就是電腦總容量了。

2、CPU序號

        Dim Wmi As New System.Management.ManagementObjectSearcher("SELECT * FROM Win32_Processor")        Dim cpuId As String        For Each WmiObj As ManagementObject In Wmi.Get            cpuId = WmiObj("ProcessorId")        Next

3、MAC地址和IP地址

        Dim netid As String = ""        Dim ip As String        Dim searcher As New ManagementObjectSearcher("select * from win32_NetworkAdapterConfiguration")        Dim moc2 As ManagementObjectCollection = searcher.Get()        For Each mo As ManagementObject In moc2            If mo("IPEnabled") Then                netid = mo("MACAddress")                ip = mo("IpAddress")(0)                Exit For            End If        Next
IpAddress得到的是含有倆個元素的數組,第一個是實際上的IP。

4、電腦系統類別型與實體記憶體

        Dim pcType As String        Dim pcMemory As String        Dim mos As New ManagementObjectSearcher("select * from Win32_ComputerSystem")        For Each mo As Object In mos.Get()            pcType = mo("SystemType")            pcMemory = mo("TotalPhysicalMemory")        Next

系統類別型,電腦的作業系統;實體記憶體,即是指運行記憶體。

比如我當前電腦配置是“x64-based PC”、“8272879616”-8G

5、其它

  Dim userName = Environment.UserName--擷取使用者名稱 eg:Admin
  Dim machineName = Environment.MachineName--擷取電腦名稱 eg:IBM777-PB4DVTY

 

C#/VB.NET 擷取電腦屬性(硬碟ID、硬碟容量、Cpu序號、MAC地址、系統類別型)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.