zoukankan      html  css  js  c++  java
  • 获取客户机的当前使用的IP、CPU、硬盘序列等信息生成机器码

    客户机信息Helper

    /**
    *┌──────────────────────────────────────────────────────────────┐
    *│ 描    述:客户端机器信息相关的工具类                                                   
    *│ 作    者:执笔小白                                              
    *│ 版    本:2.0                                       
    *│ 创建时间:2020-6-13 15:40:56                            
    *└──────────────────────────────────────────────────────────────┘
    *┌──────────────────────────────────────────────────────────────┐
    *│ 命名空间: ZhibiXiaoBai.Uril.EquipmentHelper                               
    *│ 类    名:GetlocalIpHelper                                     
    *└──────────────────────────────────────────────────────────────┘
    */
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Management;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ZhibiXiaoBai.Uril.EquipmentHelper
    {
        /// <summary>
        /// 获取本地IP、MAC、硬盘序列、机器码等
        /// </summary>
        public class GetlocalIpHelper
        {
            /// <summary>
            /// 获取本地当前使用的Ip
            /// </summary>
            /// <returns></returns>
            public static string GetlocalIp()
            {
                using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
                {
                    socket.Connect("8.8.8.8", 65530);
                    IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
                    return endPoint.Address.ToString();
                }
            }
    
            ///<summary> 
            ///获取cpu序列号     
            ///</summary> 
            ///<returns>string</returns> 
            private static string GetCpuInfo()
            {
                string cpuInfo = "";
                try
                {
                    using (ManagementClass cimobject = new ManagementClass("Win32_Processor"))
                    {
                        ManagementObjectCollection moc = cimobject.GetInstances();
    
                        foreach (ManagementObject mo in moc)
                        {
                            cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                            mo.Dispose();
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return cpuInfo.ToString();
            }
    
            ///<summary> 
            ///获取硬盘ID     
            ///</summary> 
            ///<returns> string </returns> 
            private static string GetHDid()
            {
                string HDid = "";
                try
                {
                    using (ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive"))
                    {
                        ManagementObjectCollection moc1 = cimobject1.GetInstances();
                        foreach (ManagementObject mo in moc1)
                        {
                            HDid = (string)mo.Properties["Model"].Value;
                            mo.Dispose();
                            break;
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return HDid.ToString();
            }
    
            ///<summary> 
            ///获取网卡硬件地址 
            ///</summary> 
            ///<returns> string </returns> 
            private static string GetMoAddress()
            {
                string MoAddress = "";
                try
                {
                    using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
                    {
                        ManagementObjectCollection moc2 = mc.GetInstances();
                        foreach (ManagementObject mo in moc2)
                        {
                            if ((bool)mo["IPEnabled"] == true)
                                MoAddress = mo["MacAddress"].ToString();
                            mo.Dispose();
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return MoAddress.ToString();
            }
    
            /// <summary>
            /// 机器码获取程序,随机生成一个GUID+计算机名组成形成一个唯一码。
            /// </summary>
            /// <returns></returns>
            public static string GetCode()
            {
                // 机器码处理可以如下
                string str = "PC." + GetCpuInfo() + "." + GetMoAddress();
                return str;
            }
        }
    }
  • 相关阅读:
    [转]Magento刷新索引的几种方法
    [转]centos7 移动mysql5.7.19 数据存储位置
    [转]解决Magento批量导入带图片的商品的问题
    [转]【mysql监控】查看mysql库大小,表大小,索引大小
    [转]Centos系统中查看文件和文件夹大小
    [转]Magento 2.2 Developer Documentation
    [转]Magento2开发教程
    [转]Magento Configurable Product
    [转]论magento1和magento2的速度性能优化问题
    [转]本地 Windows 计算机密码登录 登录 腾讯云 Linux 实例
  • 原文地址:https://www.cnblogs.com/qq2806933146xiaobai/p/15671968.html
Copyright © 2011-2022 走看看