威尼斯网址开户网站C#获取cpu序列号,硬盘ID,网卡MAC地址

VB.NET 获取电脑属性(硬盘ID、硬盘容量、Cpu序列号、MAC地址、系统类型)方法详解,vb.netmac

1、硬盘

在我查看网上一些文档时,发现很多人对硬盘序列号很模糊~

什么叫硬盘序列号?指的是作为一个硬盘的标识;但是有时候发现,同事的电脑硬盘序列号有重复的。所以硬盘标识我们可以试试用ID,这个如何获取下面讲解。

威尼斯网址开户网站 1威尼斯网址开户网站 2

我们可以运行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

获取电脑属性(硬盘ID、硬盘容量、Cpu序列号、MAC地址、系统类型)方法详解,vb.netmac
1、硬盘 在我查看网上一些文档时,发现很多人…

在开发过程中,经常需要获取电脑的一些属性,如获取硬盘ID/CPU序列号/MAC地址作为来加密字符串。

在开发过程中,经常需要获取电脑的一些属性,如获取硬盘ID/CPU序列号/MAC地址作为来加密字符串。

首先在添加引用中选中System.Management

方法一:nbtstat 命令

1、硬盘

1、硬盘

再在开始部门:

Public Function GetMac(ByVal IP As String) As String

在我查看网上一些文档时,发现很多人对硬盘序列号很模糊~

在我查看网上一些文档时,发现很多人对硬盘序列号很模糊~

using System.Management;

       Try

什么叫硬盘序列号?指的是作为一个硬盘的标识;但是有时候发现,同事的电脑硬盘序列号有重复的。所以硬盘标识我们可以试试用ID,这个如何获取下面讲解。

什么叫硬盘序列号?指的是作为一个硬盘的标识;但是有时候发现,同事的电脑硬盘序列号有重复的。所以硬盘标识我们可以试试用ID,这个如何获取下面讲解。

using System.Management.Instrumentation;

            定义输出变量。

威尼斯网址开户网站 3威尼斯网址开户网站 4

威尼斯网址开户网站 3威尼斯网址开户网站 4

private void GetInfo()  

            Dim StrMac As String = String.Empty

我们可以运行DOS命令,或者查看注册表,查看硬盘的序列号。如图中所示:都是HGST
HTS725050A7E630。

我们可以运行DOS命令,或者查看注册表,查看硬盘的序列号。如图中所示:都是HGST
HTS725050A7E630。

  {  

            Dim Str As String = String.Empty

大部分情况下,大家都可以通过如下获取:

大部分情况下,大家都可以通过如下获取:

   string cpuInfo = “”;//cpu序列号  

            定义进程参数。

        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")
        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")

   ManagementClass cimobject = new
ManagementClass(“Win32_Processor”);  

            Dim Info As New ProcessStartInfo

也可以通过Wmi方式获取:

也可以通过Wmi方式获取:

   ManagementObjectCollection moc = cimobject.GetInstances();  

            Info.FileName = “nbtstat”

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

   foreach(ManagementObject mo in moc)  

            Info.Arguments = “-a ” + IP

个人建议用Wmiy中的signature来作为ID,毕竟是数字,且不重复。

个人建议用Wmiy中的signature来作为ID,毕竟是数字,且不重复。

   {  

            Info.UseShellExecute = False

PS:顺带提一个硬盘的容量,通过属性size可以获取单个硬盘的大小,加起来的话就是电脑总容量了。

PS:顺带提一个硬盘的容量,通过属性size可以获取单个硬盘的大小,加起来的话就是电脑总容量了。

    cpuInfo = mo.Properties[“ProcessorId”].Value.ToString();  

            Info.RedirectStandardInput = False

2、CPU序列号

2、CPU序列号

    Response.Write (“cpu序列号:”+cpuInfo.ToString ());  

            Info.RedirectStandardOutput = True

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

   }  

            Info.RedirectStandardError = True

3、MAC地址和IP地址

3、MAC地址和IP地址

   //获取硬盘ID  

            Info.CreateNoWindow = True

        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。
        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。

   String HDid;  

            Info.WindowStyle = ProcessWindowStyle.Hidden

4、电脑系统类型与物理内存

4、电脑系统类型与物理内存

   ManagementClass cimobject1 = new
ManagementClass(“Win32_DiskDrive”);  

            定义新进程。

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

   ManagementObjectCollection moc1 = cimobject1.GetInstances();  

            Dim mProcess As New Process

系统类型,电脑的操作系统;物理内存,即是指运行内存。

系统类型,电脑的操作系统;物理内存,即是指运行内存。

   foreach(ManagementObject mo in moc1)  

            开始执行操作。

比如我当前电脑配置是“x64-based PC”、“8272879616”-8G

比如我当前电脑配置是“x64-based PC”、“8272879616”-8G

   {  

            mProcess = Process.Start(Info)

5、其它

5、其它

    HDid = (string)mo.Properties[“Model”].Value;  

            无限循环按行读取输出内容。

  Dim userName = Environment.UserName–获取用户名 eg:Admin
  Dim machineName = Environment.MachineName–获取电脑名称
eg:IBM777-PB4DVTY

  Dim userName = Environment.UserName–获取用户名 eg:Admin
  Dim machineName = Environment.MachineName–获取电脑名称
eg:IBM777-PB4DVTY

    Response.Write (“硬盘序列号:”+HDid.ToString ());  

            While True

 

   }  

                读取一行数据。

   //获取网卡硬件地址  

                Str = mProcess.StandardOutput.ReadLine()

   ManagementClass mc = new
ManagementClass(“Win32_NetworkAdapterConfiguration”);  

                mProcess.WaitForExit()

   ManagementObjectCollection moc2 = mc.GetInstances();  

                StrMac = Str.Trim()

   foreach(ManagementObject mo in moc2)  

                判断是否有 Mac Address字眼。

   {  

                If StrMac.ToLower.Contains(“mac address”) Then

    if((bool)mo[“IPEnabled”] == true)  

                    Exit While

     Response.Write(“MAC
address\t{0}”+mo[“MacAddress”].ToString());  

                Else

    mo.Dispose();  

                    If StrMac.ToLower.Contains(“host not found”) Then

   }  

                        Exit While

  }

                    End If

                End If

            End While

            返回值。

            Return StrMac

        Catch ex As Exception

            返回错误值。

            Return String.Format(“Get Error. 原因:{0}”, ex.Message)

        End Try

 

方法2:SendARP 函数

Private Declare Ansi Function SendARP Lib “Iphlpapi.dll” (ByVal dest As
Int32, ByVal host As Int32, ByRef mac As Int64, ByRef length As Int32)
As Int32

    Private Declare Ansi Function inet_addr Lib “Ws2_32.dll” (ByVal IP
As String) As Int32

    Public Function getRemoteMAC(ByVal RemoteIp As String) As String

        Dim Dest As Int32 = inet_addr(RemoteIp)

        Dim Str As String = String.Empty

        Dim StrMac As String = String.Empty

        Try

            Dim MacInfo As New Int64

            Dim Len As Int32 = 6

            Dim Res As Integer = SendARP(Dest, 0, MacInfo, Len)

            Str = Convert.ToString(MacInfo, 16).ToUpper

            If Str.Length = 12 Then

                因为取出来的值是反的,所以要处理下。

                Dim M(5) As String

                M(0) = Str.Substring(10, 2)

                M(1) = Str.Substring(8, 2)

                M(2) = Str.Substring(6, 2)

                M(3) = Str.Substring(4, 2)

                M(4) = Str.Substring(2, 2)

                M(5) = Str.Substring(0, 2)

                StrMac = String.Format(“{0}-{1}-{2}-{3}-{4}-{5}”, M(0),
M(1), M(2), M(3), M(4), M(5))

            Else

                StrMac = “Host not found.”

            End If

        Catch ex As Exception

            返回错误值。

            Return String.Format(“Get Error. 原因:{0}”, ex.Message)

        End Try

        Return StrMac

    End Function

 

方法3:用WMI,先工程-引用 Microsoft WMI Scripting V1.1 Library

Private Sub wmiSoundDeviceInfo()

 

        Dim wmiObjSet As WbemScripting.SWbemObjectSet

        Dim obj As SWbemObject

 

        wmiObjSet =
GetObject(“winmgmts:{impersonationLevel=impersonate}”). _

        InstancesOf(“Win32_Networ

命令 Public Function GetMac(ByVal
IP As String) As String Try 定义输出变量。 Dim StrMac As String =
String.Empty Dim Str As String = String.Empty 定义进…

相关文章