C#提取硬件信息HardwareHelper

 using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Management;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;

namespace Core.Helpers
{
  public static class HardwareHelper
  {
    private static string hardwareUniqueID { get; set; }

    public static string GetHardwareUniqueID()
    {
      if (string.IsNullOrEmpty(HardwareHelper.hardwareUniqueID))
      {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.Append("UUID:" + HardwareHelper.GetUUID() + ";");
        stringBuilder.Append("CPU:" + HardwareHelper.GetCpuId() + ";");
        stringBuilder.Append("BIOS:" + HardwareHelper.GetBiosId() + ";");
        stringBuilder.Append("BASE:" + HardwareHelper.GetBaseId() + ";");
        stringBuilder.Append("Disk:" + HardwareHelper.GetHardDiskSerialNumber() + ";");
        stringBuilder.Append("MAC:" + HardwareHelper.GetMacAddress() + ";");
        HardwareHelper.hardwareUniqueID = MD5Helper.GetStringMd5(stringBuilder.ToString());
      }
      return HardwareHelper.hardwareUniqueID;
    }

    public static string GetMacAddress()
    {
      List macAddressList = HardwareHelper.GetMacAddressList();
      return macAddressList.Count > 0 ? macAddressList[0] : "";
    }

    public static List GetMacAddressList()
    {
      List macAddressList = new List();
      foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
      {
        if (networkInterface.OperationalStatus == OperationalStatus.Up && networkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback && !macAddressList.Contains(networkInterface.GetPhysicalAddress().ToString()))
          macAddressList.Add(networkInterface.GetPhysicalAddress().ToString());
      }
      return macAddressList;
    }

    public static string GetLocalIPv4()
    {
      List localIpv4List = HardwareHelper.GetLocalIPv4List();
      return localIpv4List.Count > 0 ? localIpv4List[0] : "";
    }

    public static List GetLocalIPv4List()
    {
      List localIpv4List = new List();
      foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
      {
        string name = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + networkInterface.Id + "\\Connection";
        RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(name, false);
        if (registryKey != null)
        {
          string str = registryKey.GetValue("PnpInstanceID", (object) "").ToString();
          Convert.ToInt32(registryKey.GetValue("MediaSubType", (object) 0));
          if (str.Length > 3 && str.Substring(0, 3) == "PCI")
          {
            foreach (UnicastIPAddressInformation unicastAddress in networkInterface.GetIPProperties().UnicastAddresses)
            {
              if (unicastAddress.Address.AddressFamily == AddressFamily.InterNetwork && unicastAddress.IsDnsEligible)
                localIpv4List.Add(unicastAddress.Address.ToString());
            }
          }
        }
      }
      return localIpv4List;
    }

    public static string GetUUID()
    {
      string uuid = (string) null;
      using (ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("select * from Win32_ComputerSystemProduct"))
      {
        foreach (ManagementBaseObject managementBaseObject in managementObjectSearcher.Get())
        {
          using (managementBaseObject)
            uuid = managementBaseObject["UUID"].ToString();
        }
      }
      return uuid;
    }

    public static string GetHardDiskSerialNumber()
    {
      try
      {
        ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
        string diskSerialNumber = "";
        using (ManagementObjectCollection.ManagementObjectEnumerator enumerator = managementObjectSearcher.Get().GetEnumerator())
        {
          if (enumerator.MoveNext())
            diskSerialNumber = enumerator.Current["SerialNumber"].ToString().Trim();
        }
        return diskSerialNumber;
      }
      catch
      {
        return "";
      }
    }

    private static string Identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue)
    {
      string str = "";
      foreach (ManagementObject instance in new ManagementClass(wmiClass).GetInstances())
      {
        if (instance[wmiMustBeTrue].ToString() == "True")
        {
          if (str == "")
          {
            try
            {
              str = instance[wmiProperty].ToString();
              break;
            }
            catch
            {
            }
          }
        }
      }
      return str;
    }

    private static string Identifier(string wmiClass, string wmiProperty)
    {
      string str = "";
      foreach (ManagementObject instance in new ManagementClass(wmiClass).GetInstances())
      {
        if (str == "")
        {
          try
          {
            str = instance[wmiProperty].ToString();
            break;
          }
          catch
          {
          }
        }
      }
      return str;
    }

    public static string GetCpuId()
    {
      string cpuId = HardwareHelper.Identifier("Win32_Processor", "UniqueId");
      if (cpuId == "")
      {
        cpuId = HardwareHelper.Identifier("Win32_Processor", "ProcessorId");
        if (cpuId == "")
        {
          string str = HardwareHelper.Identifier("Win32_Processor", "Name");
          if (str == "")
            str = HardwareHelper.Identifier("Win32_Processor", "Manufacturer");
          cpuId = str + HardwareHelper.Identifier("Win32_Processor", "MaxClockSpeed");
        }
      }
      return cpuId;
    }

    public static string GetBiosId() => HardwareHelper.Identifier("Win32_BIOS", "Manufacturer") + HardwareHelper.Identifier("Win32_BIOS", "SMBIOSBIOSVersion") + HardwareHelper.Identifier("Win32_BIOS", "IdentificationCode") + HardwareHelper.Identifier("Win32_BIOS", "SerialNumber") + HardwareHelper.Identifier("Win32_BIOS", "ReleaseDate") + HardwareHelper.Identifier("Win32_BIOS", "Version");

    public static string GetBaseId() => HardwareHelper.Identifier("Win32_BaseBoard", "Model") + HardwareHelper.Identifier("Win32_BaseBoard", "Manufacturer") + HardwareHelper.Identifier("Win32_BaseBoard", "Name") + HardwareHelper.Identifier("Win32_BaseBoard", "SerialNumber");
  }
}
 

你可能感兴趣的:(windows)