C#获取ip地址、dns、mac地址、cpu信息

/// 
    /// 使用 C# 自带的类库实现计算机信息获取
    /// 
    public class DefaultDeviceInfo 
    {
        public virtual string GetCpuId()
        {
            try
            {
                string cpuInfo = " ";
                ManagementClass cimobject = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = cimobject.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }
                return cpuInfo.ToString();
            }
            catch (Exception)
            {

            }

            return string.Empty;
        }

        public virtual string GetHDid()
        {
            try
            {
                string HDid = " ";
                ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
                ManagementObjectCollection moc1 = cimobject1.GetInstances();
                foreach (ManagementObject mo in moc1)
                {
                    HDid = (string)mo.Properties["Model"].Value;
                }
                return HDid.ToString();
            }
            catch (Exception)
            {
            }
            return string.Empty;
        }

        public virtual string GetIpv4()
        {
            string ip = GetIpv4("127.0.0.1");
            //如果根据127.0.0.1无法获取ip地址,则尝试根据主机名获取
            if (ip == IPAddress.None.ToString())
            {
                ip = GetIpv4(null);
            }

            return ip;
        }

        public virtual string GetMac()
        {
            try
            {
                List macs = new List();
                NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface ni in interfaces)
                {
                    if (ni.NetworkInterfaceType != NetworkInterfaceType.Ethernet)
                    {
                        continue;
                    }

                    if (ni.GetPhysicalAddress().ToString() != "")
                    {
                        macs.Add(ni.GetPhysicalAddress().ToString());
                    }
                }

                //替补mac地址,当找不到以太网mac,则使用第一个mac
                var subs = macs.Count == 0 && interfaces.Length > 0
                    ? interfaces[0].GetPhysicalAddress().ToString()
                    : string.Empty;

                return macs.Count > 0 ? macs[0] : subs;
            }
            catch (Exception)
            {

            }

            return string.Empty;
        }

        public virtual string GetHostName()
        {
            return Dns.GetHostName();
        }

        #region 内部方法

        /// 
        /// 字符串每隔多少位插入特定字符
        /// 
        /// 
        /// 
        /// 
        /// 
        protected static string StringInsertSeparator(string value, string separator, int separatorLen = 1)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return value;
            }

            StringBuilder sbNewValue = new StringBuilder();

            for (int i = 0; i < value.ToArray().Count(); i++)
            {
                if (i > 0 && i % separatorLen == 0)
                {
                    sbNewValue.Append(separator);
                }

                sbNewValue.Append(value[i]);
            }

            return sbNewValue.ToString();
        }

        /// 
        /// 根据指定的主机名获取ip地址
        /// 如果主机名为空,则获取系统主机名称
        /// 
        /// 
        /// 
        protected string GetIpv4(string defHostName)
        {
            try
            {
                /* NetworkInterface 实现方法
                // 获得网络接口,网卡,拨号器,适配器都会有一个网络接口 
                NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

                foreach (NetworkInterface network in networkInterfaces)
                {
                    if (network.NetworkInterfaceType != NetworkInterfaceType.Ethernet)
                    {
                        continue;
                    }

                    // 获得当前网络接口属性
                    IPInterfaceProperties properties = network.GetIPProperties();

                    // 每个网络接口可能会有多个IP地址 
                    foreach (IPAddressInformation address in properties.UnicastAddresses)
                    {
                        // 如果此IP不是ipv4,则进行下一次循环
                        if (address.Address.AddressFamily != AddressFamily.InterNetwork)
                        {
                            continue;
                        }

                        // 忽略127.0.0.1
                        if (IPAddress.IsLoopback(address.Address))
                        {
                            continue;
                        }

                        return address.Address.ToString();
                    }
                }

                */

                string hostName = defHostName;
                if (string.IsNullOrWhiteSpace(hostName))
                {
                    hostName = Dns.GetHostName();
                }

                var ipList = Dns.GetHostEntry(hostName).AddressList;

                foreach (var address in ipList)
                {
                    // 如果此IP不是ipv4,则进行下一次循环
                    if (address.AddressFamily != AddressFamily.InterNetwork)
                    {
                        continue;
                    }

                    // 忽略127.0.0.1
                    if (IPAddress.IsLoopback(address))
                    {
                        continue;
                    }

                    return address.ToString();
                }

            }
            catch (Exception ex)
            {
                
            }

            return IPAddress.None.ToString(); //255.255.255.255 
        }

        #endregion

    }

你可能感兴趣的:(C#)