您的位置:首页 > 编程语言 > VB

C#/VB.NET 获取电脑属性(硬盘ID、硬盘容量、Cpu序列号、MAC地址、系统类型)

2016-11-03 19:15 696 查看
在开发过程中,经常需要获取电脑的一些属性,如获取硬盘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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐