• 计算机硬件通用功能类:硬件信息控制器(主机名,cpu编号,网卡地址,MAC地址,主硬盘编号,ip地址,获取最大线程数,验证服务IP)


     需要添加引用VS自带的一个类库:System.Management.dll

    C#通用功能类:

      硬件信息控制器(主机名,cpu编号,网卡地址,MAC地址,主硬盘编号,ip地址,获取最大线程数,验证服务IP)

    namespace TeamFoundation.SystemManage.Client.Win.Core
    {
        /// <summary>
        /// 硬件信息控制器
        /// </summary>
        public class HardWareHandler
        {
            /// <summary>
            /// 机器CPU内核数
            /// </summary>
            public static int CpuCount { get { return System.Environment.ProcessorCount; } }
            #region Public Methods
            /// <summary>
            /// 获取本地主机名
            /// </summary>
            /// <returns></returns>
            public static string GetHostName()
            {
                return System.Net.Dns.GetHostName();
            }
    
            /// <summary>
            /// 获取CPU编号
            /// </summary>
            /// <returns></returns>
            public static string GetCpuId()
            {
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                String strCpuID = string.Empty;
                foreach (ManagementObject mo in moc)
                {
                    if (mo.Properties["ProcessorId"] != null)
                    {
                        strCpuID = mo.Properties["ProcessorId"].Value.ToString();
                        break;
                    }
                }
                return strCpuID;
            }
    
            /// <summary>
            /// 获取网卡地址
            /// </summary>
            /// <returns></returns>
            public static string[] GetNetCardMacAddressArray()
            {
                string macAddress = GetNetCardMacAddress();
                if (macAddress.Length > 0)
                {
                    List<string> lstMac = new List<string>();
                    string[] arrMac = macAddress.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    foreach (string mac in arrMac)
                    {
                        if (!lstMac.Contains(mac))
                            lstMac.Add(mac);
                    }
                    return lstMac.ToArray();
                }
                else
                {
                    return null;
                }
            }
    
            /// <summary>
            /// 获取MAC地址
            /// </summary>
            /// <returns></returns>
            public static string GetNetCardMacAddress()
            {
                try
                {
                    ManagementClass mc;
                    ManagementObjectCollection moc;
                    mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                    moc = mc.GetInstances();
                    string str = "";
                    foreach (ManagementObject mo in moc)
                    {
                        if ((bool)mo["IPEnabled"] == true)
                            str += "," + mo["MacAddress"].ToString();
                    }
                    return str.Length > 0 ? str.Substring(1) : str;
                }
                catch
                {
                    return "";
                }
            }
    
            /// <summary>
            /// 获取主硬盘编号
            /// </summary>
            /// <returns></returns>
            public static string GetMainHardDiskId()
            {
                try
                {
                    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
                    String strHardDiskID = string.Empty;
                    foreach (ManagementObject mo in searcher.Get())
                    {
                        strHardDiskID = mo["SerialNumber"].ToString().Trim();
                        if (!string.IsNullOrEmpty(strHardDiskID))
                            break;
                    }
                    return strHardDiskID;
                }
                catch
                {
                    return string.Empty;
                }
            }
    
            /// <summary>
            /// 获取IP地址
            /// </summary>
            /// <returns></returns>
            public static string GetIpAddress()
            {
                try
                {
                    IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
                    if (ips != null && ips.Length > 0)
                    {
                        foreach (IPAddress ip in ips)
                        {
                            if (ip.AddressFamily.ToString().Equals("InterNetwork"))
                                return ip.ToString();
                        }
                    }
                }
                catch
                {
                }
                return string.Empty;
            }
            #endregion
            /// <summary>
            /// 获取最大线程数
            /// </summary>
            /// <returns></returns>
            public static int GetMaxThreads()
            {
                int threadCount = 4;
                if (CpuCount > 1)
                {
                    threadCount = CpuCount * 2;
                }
                threadCount = threadCount > 8 ? 8 : threadCount;
                return threadCount;
            }
    
            /// <summary>
            /// 验证服务端IP
            /// </summary>
            /// <returns></returns>
            public static bool ValidateIpAddress(string ip, out string msg)
            {
                msg = string.Empty;
                string strReg = @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$";
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(strReg);
                if (!regex.IsMatch(ip))
                {
                    msg = "配置的IP地址格式不正确!";
                    return false;
                }
                return true;
            }
            /// <summary>
            /// 验证端口号
            /// </summary>
            /// <param name="intNum"></param>
            /// <param name="msg"></param>
            /// <returns></returns>
            public static bool ValidateNum(int intNum, out string msg)
            {
                msg = string.Empty;
                string strReg = @"^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$";
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(strReg);
                if (!regex.IsMatch(intNum.ToString()))
                {
                    msg = "服务端口请输入0到65535的整数";
                    return false;
                }
                return true;
            }
        }
    }
  • 相关阅读:
    简单聊聊智能硬件的固件测试
    Regular进阶: 跨组件通信
    项目组建和磨合阶段常见的坑
    从需求到数据到改进,如何形成闭环
    请不要怪罪流程
    自备干货!如何有效的做竞品迭代分析
    css小点心
    java的collection集合
    java中的数组与集合相互转换
    java-增强for循环
  • 原文地址:https://www.cnblogs.com/lqsilly/p/2921680.html
Copyright © 2020-2023  润新知