C#电脑软件注册之获取硬件序列号

 计算机的CPU、主板、BIOS、MAC地址记录下来

控制台查看电脑磁盘序列号

  1. cmd
  2. diskpart
  3. list disk
  4. select disk 0
  5. detail disk

第二行就是序列号

 

控制台查看电脑CPU序列号

  1. cmd
  2. wmic CPU get ProcessorID

 查看MAC

 

public class ComputerInfo
{
    public static string GetComputerInfo()
    {
        string info = string.Empty;
        string cpu = GetCPUInfo();
        string baseBoard = GetBaseBoardInfo();
        string bios = GetBIOSInfo();
        string mac = GetMACInfo();
        info = string.Concat(cpu, baseBoard, bios, mac);
        return info;
    }
 
    private static string GetCPUInfo()
    {
        string info = string.Empty;
        info = GetHardWareInfo("Win32_Processor", "ProcessorId");
        return info;
    }
    private static string GetBIOSInfo()
    {
        string info = string.Empty;
        info = GetHardWareInfo("Win32_BIOS", "SerialNumber");
        return info;
    }
    private static string GetBaseBoardInfo()
    {
        string info = string.Empty;
        info = GetHardWareInfo("Win32_BaseBoard", "SerialNumber");
        return info;
    }
    private static string GetMACInfo()
    {
        string info = string.Empty;
        info = GetHardWareInfo("Win32_BaseBoard", "SerialNumber");
        return info;
    }
    private static string GetHardWareInfo(string typePath, string key)
    {
        try
        {
            ManagementClass managementClass = new ManagementClass(typePath);
            ManagementObjectCollection mn = managementClass.GetInstances();
            PropertyDataCollection properties = managementClass.Properties;
            foreach (PropertyData property in properties)
            {
                if (property.Name == key)
                {
                    foreach (ManagementObject m in mn)
                    {
                        return m.Properties[property.Name].Value.ToString();
                    }
                }
 
            }
        }
        catch (Exception ex)
        {
            //这里写异常的处理  
        }
        return string.Empty;
    }
    private static string GetMacAddressByNetworkInformation()
    {
        string key = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\";
        string macAddress = string.Empty;
        try
        {
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
                if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet
                    && adapter.GetPhysicalAddress().ToString().Length != 0)
                {
                    string fRegistryKey = key + adapter.Id + "\\Connection";
                    RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
                    if (rk != null)
                    {
                        string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
                        int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
                        if (fPnpInstanceID.Length > 3 &&
                            fPnpInstanceID.Substring(0, 3) == "PCI")
                        {
                            macAddress = adapter.GetPhysicalAddress().ToString();
                            for (int i = 1; i < 6; i++)
                            {
                                macAddress = macAddress.Insert(3 * i - 1, ":");
                            }
                            break;
                        }
                    }
 
                }
            }
        }
        catch (Exception ex)
        {
            //这里写异常的处理  
        }
        return macAddress;
    }
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值