• 超强获取计算机信息类(可获取cpu序列号,网卡地址,ip地址,硬盘id,登录系统用户,计算机名等等)


    using System;
    using System.Management;
    namespace ZGX.Common
    {
        /// <summary>   
        /// Computer Information   
        /// </summary>   
        public class Computer
        {
            public string CpuID;
            public string MacAddress;
            public string DiskID;
            public string IpAddress;
            public string LoginUserName;
            public string ComputerName;
            public string SystemType;
            public string TotalPhysicalMemory; //单位:M   
            private static Computer _instance;
            public static Computer Instance()
            {
                if (_instance == null)
                    _instance = new Computer();
                return _instance;
            }
            /// <summary>
            /// 构造函数
            /// </summary>
            public Computer()
            {
                CpuID = GetCpuID();
                MacAddress = GetMacAddress();
                DiskID = GetDiskID();
                IpAddress = GetIPAddress();
                LoginUserName = GetUserName();
                SystemType = GetSystemType();
                TotalPhysicalMemory = GetTotalPhysicalMemory();
                ComputerName = GetComputerName();
            }
            /// <summary>
            /// 获取cpu序列号
            /// </summary>
            /// <returns></returns>
            string GetCpuID()
            {
                try
                {
                    //获取CPU序列号代码   
                    string cpuInfo = "";//cpu序列号   
                    ManagementClass mc = new ManagementClass("Win32_Processor");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                    }
                    moc = null;
                    mc = null;
                    return cpuInfo;
                }
                catch
                {
                    return "unknow";
                }
                finally
                {
                }

            }
            /// <summary>
            /// 获取网卡硬件地址  
            /// </summary>
            /// <returns></returns>
            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;
                        }
                    }
                    moc = null;
                    mc = null;
                    return mac;
                }
                catch
                {
                    return "unknow";
                }
                finally
                {
                }

            }
            /// <summary>
            /// 获取IP地址
            /// </summary>
            /// <returns></returns>
            string GetIPAddress()
            {
                try
                {
                    //获取IP地址   
                    string st = "";
                    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        if ((bool)mo["IPEnabled"] == true)
                        {
                            //st=mo["IpAddress"].ToString();   
                            System.Array ar;
                            ar = (System.Array)(mo.Properties["IpAddress"].Value);
                            st = ar.GetValue(0).ToString();
                            break;
                        }
                    }
                    moc = null;
                    mc = null;
                    return st;
                }
                catch
                {
                    return "unknow";
                }
                finally
                {
                }

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

            }

            /// <summary>   
            /// 操作系统的登录用户名   
            /// </summary>   
            /// <returns></returns>   
            string GetUserName()
            {
                try
                {
                    string st = "";
                    ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {

                        st = mo["UserName"].ToString();

                    }
                    moc = null;
                    mc = null;
                    return st;
                }
                catch
                {
                    return "unknow";
                }
                finally
                {
                }

            }


            /// <summary>   
            /// PC类型   
            /// </summary>   
            /// <returns></returns>   
            string GetSystemType()
            {
                try
                {
                    string st = "";
                    ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {

                        st = mo["SystemType"].ToString();

                    }
                    moc = null;
                    mc = null;
                    return st;
                }
                catch
                {
                    return "unknow";
                }
                finally
                {
                }

            }

            /// <summary>   
            /// 物理内存   
            /// </summary>   
            /// <returns></returns>   
            string GetTotalPhysicalMemory()
            {
                try
                {

                    string st = "";
                    ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {

                        st = mo["TotalPhysicalMemory"].ToString();

                    }
                    moc = null;
                    mc = null;
                    return st;
                }
                catch
                {
                    return "unknow";
                }
                finally
                {
                }
            }
            /// <summary>   
            ///    电脑名称
            /// </summary>   
            /// <returns></returns>   
            string GetComputerName()
            {
                try
                {
                    return System.Environment.GetEnvironmentVariable("ComputerName");
                }
                catch
                {
                    return "unknow";
                }
                finally
                {
                }
            }


        }
    }

  • 相关阅读:
    2016/2/22 1、DOM的基本概念 2、Window对象操作 3、Windows.history对象 4、Window.location对象 5、Window.status对象
    2016/2/21 JavaScript简介
    2016/2/19 position: fixed absolute relative z-index float 半透明效果
    2016/2/19 css样式表 Cascading Style Sheet 叠层样式表 美化HTML网页
    2016/2/18 html 图片热点,网页划区,拼接,表单
    2016/2/18 html 标签 表格
    重载
    面向对象 Java练习
    Car 加油
    大乐透 Java随机码
  • 原文地址:https://www.cnblogs.com/top5/p/2038797.html
Copyright © 2020-2023  润新知