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地址

 1     public string GetMacAddress()
 2     {
 3         string macAddress = "";
 4         ManagementObjectSearcher query = new ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration");
 5         ManagementObjectCollection querylist = query.Get();
 6         foreach (var managementBaseObject in querylist)
 7         {
 8             if (managementBaseObject["IPEnabled"].ToString() == "True")
 9             {
10                 macAddress = managementBaseObject["MacAddress"].ToString();
11             }
12         }
13         return macAddress;
14     }
15     string GetAddressIp()
16     {
17         string addressIp = string.Empty;
18         var ipHostEntry = Dns.GetHostEntry(Dns.GetHostName());
19         foreach (IPAddress iPAddress in ipHostEntry.AddressList)
20         {
21             if (iPAddress.AddressFamily.ToString() == "InterNetwork")
22             {
23                 addressIp = iPAddress.ToString();
24             }
25         }
26         return addressIp;
27     }

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

 

posted @ 2016-11-03 19:15  唐宋元明清2188  阅读(5442)  评论(0编辑  收藏  举报