C#.Net获取Mac等PC信息

止乎于分享

 

using System.Management;
using System.Net;

 

    public class PCInfoHelper
    {
        /// <summary>
        /// 操作系统的登录用户名
        /// </summary>
        /// <returns></returns>
        public static string GetUserName()
        {
            try
            {
                string username = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    username = mo["UserName"].ToString();
                }
                return username;
            }
            catch
            {
                return "unknow";
            }
        }

        /// <summary>
        /// 获取MAC地址
        /// </summary>
        /// <returns></returns>
        public static string GetMacAddress()
        {
            try
            {
                string mac = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        mac = mo["MacAddress"].ToString();
                        break;
                    }
                }
                return mac;
            }
            catch
            {
                return "unknow";
            }
        }

        /// <summary>
        /// 获取硬盘ID
        /// </summary>
        /// <returns></returns>
        public static string GetDiskID()
        {
            try
            {
                String diskid = "";
                ManagementClass mc = new ManagementClass("Win32_DiskDrive");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    diskid = (String)mo.Properties["Model"].Value.ToString();
                }
                return diskid;
            }
            catch
            {
                return "unknow";
            }
        }

        /// <summary>
        /// 获取CPU ID
        /// </summary>
        /// <returns></returns>
        public static string GetCpuID()
        {
            try
            {
                string cpuid = "";
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuid = mo.Properties["ProcessorId"].Value.ToString();
                }
                return cpuid;
            }
            catch
            {
                return "unknow";
            }
        }

        /// <summary>
        /// 系统名称
        /// </summary>
        /// <returns></returns>
        public static string GetSystemType()
        {
            try
            {
                string systype = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    systype = mo["SystemType"].ToString();
                }
                return systype;
            }
            catch
            {
                return "unknow";
            }
        }

        /// <summary>
        /// 获取物理内存
        /// </summary>
        /// <returns></returns>
        public static string GetTotalPhysicalMemory()
        {
            try
            {
                string phymemory = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    phymemory = mo["TotalPhysicalMemory"].ToString();
                }
                return phymemory;
            }
            catch
            {
                return "unknow";
            }
        }

        /// <summary>
        /// 获取客户端内网IP地址
        /// </summary>
        /// <returns></returns>
        public static string GetLocalIPAddress()
        {
            try
            {
                string localIP = null;
                IPHostEntry ipHost = System.Net.Dns.GetHostEntry(Dns.GetHostName());
                IPAddress ipaddress = ipHost.AddressList[0];
                localIP = ipaddress.ToString();
                return "内网IP地址:" + localIP;
            }
            catch
            {
                return "内网IP地址:unknown";
            }
        }

        /// <summary>
        /// 获得客户端外网IP地址
        /// </summary>
        /// <returns>IP地址</returns>
        public static string GetInternetIPAddress()
        {
            try
            {
                string internetIP = "";
                using (WebClient webClient = new WebClient())
                {
                    internetIP = webClient.DownloadString("http://www.coridc.com/ip");//从外部网页获得IP地址
                    //判断IP是否合法
                    if (!System.Text.RegularExpressions.Regex.IsMatch(internetIP, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
                    {
                        internetIP = webClient.DownloadString("http://fw.qq.com/ipaddress");//从腾讯提供的API中获得IP地址
                    }
                }
                return "外网IP地址:" + internetIP;
            }
            catch
            {
                return "外网IP地址:unknown";
            }
        }
    }

C#.Net获取Mac等PC信息,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。