首页 > 代码库 > 计算机信息类ComputerInfo

计算机信息类ComputerInfo

using System;using System.Management;using System.Net;using System.Net.Sockets;using System.Text;using System.Diagnostics;using System.Text.RegularExpressions;namespace BaseFunction{    ///<summary>    ///计算机信息类    ///</summary>    public  class ComputerInfo    {        private  string CpuID;        private string MacAddress;        private string DiskID;        private string IpAddress;        private string LoginUserName;        private string ComputerName;        private string SystemType;        private string TotalPhysicalMemory; //单位:M         private static ComputerInfo _instance;        internal static ComputerInfo Instance()        {            if (_instance == null)                _instance = new ComputerInfo();            return _instance;        }        internal ComputerInfo()        {            CpuID = GetCpuID();            MacAddress = GetMacAddress();            DiskID = GetDiskID();            IpAddress = GetIPAddress();            LoginUserName = GetUserName();            SystemType = GetSystemType();            TotalPhysicalMemory = GetTotalPhysicalMemory();            ComputerName = GetComputerName();        }        /// <summary>        /// 浏览器客户端  获取网卡MAC地址MD5加密串  杨旭东        /// </summary>        /// <returns></returns>        public static string GetClientMac()        {            try            {              string clientIP =System.Web.HttpContext.Current.Request.UserHostAddress.Trim();              Int32 idest = API.inet_addr(clientIP);              Int64 macInfo = new Int64();              Int32 length = 6;              int res = API.SendARP(idest, 0, ref macInfo, ref length);              string mac_src = http://www.mamicode.com/macInfo.ToString("X");              if (!string.IsNullOrEmpty(mac_src) && !"0".Equals(mac_src))              {                  while (mac_src.Length < 12)                  {                      mac_src = mac_src.Insert(0, "0");                  }                  string mac_dest = string.Empty;                  for (int i = 0; i < 11; i++)                  {                      if (i % 2 == 0)                      {                          if (i == 10)                              mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));                          else                              mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));                      }                  }                  return mac_dest;              }            }            catch            {                return "0";            }            return "0";        }        /// <summary>        /// 获取CPU序列号代码         /// </summary>        /// <returns></returns>      public  static  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>      public static string GetMacAddress()        {            try            {                //获取网卡硬件地址             return Mac.GetMacAddress();                            }            catch            {                return "unknow";            }            finally            {            }        }        /// <summary>        /// 获取IP地址(IPv4)        /// </summary>        /// <returns></returns>      public static string GetIPAddress()        {            try            {                IPAddress[] arrIPAddresses = Dns.GetHostAddresses(Dns.GetHostName());                foreach (IPAddress ip in arrIPAddresses)                {                    if (ip.AddressFamily.Equals(AddressFamily.InterNetwork))//IPv4                    {                        return ip.ToString();                    }                }                return "unknow";            }            catch            {                return "unknow";            }            finally            {            }        }        /// <summary>        /// 获取硬盘ID         /// </summary>        /// <returns></returns>      public static string GetDiskID()        {            try            {             return Win32.GetHddInformation().ModuleNumber;            }            catch            {                return "unknow";            }            finally            {            }        }        ///<summary>        ///操作系统的登录用户名         ///</summary>        ///<returns></returns>      public static string GetUserName()        {            try            {                byte[] userName = new byte[30];                Int32[] length = new Int32[1];                length[0] = 30;//限定用户名长度                API.GetUserName(userName, length);                return System.Text.Encoding.ASCII.GetString(userName);            }            catch            {                return "unknow";            }            finally            {            }        }        ///<summary>        /// PC类型         ///</summary>        ///<returns></returns>      public static 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>      public static string GetTotalPhysicalMemory()        {            try            {                API.MEMORY_INFO memoryInfo = new API.MEMORY_INFO();                API.GlobalMemoryStatus(ref memoryInfo);                return memoryInfo.dwTotalPhys.ToString();            }            catch            {                return "unknow";            }            finally            {            }        }        ///<summary>        /// 获取计算机名称        ///</summary>        ///<returns></returns>      public static string GetComputerName()        {            try            {                byte[] computerName = new byte[30];                Int32[] length = new Int32[1];                length[0] = 30;//限定计算机名长度                API.GetComputerName(computerName,length);                return System.Text.Encoding.ASCII.GetString(computerName);            }            catch            {                return "unknow";            }            finally            {            }        }    }}

API是一个类:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices; namespace BaseFunction{    class API    {        [DllImport("kernel32")]//内存        public static extern void GlobalMemoryStatus(ref  MEMORY_INFO meminfo);         [StructLayout(LayoutKind.Sequential)]        public struct CPU_INFO        {            public uint dwOemId;            public uint dwPageSize;            public uint lpMinimumApplicationAddress;            public uint lpMaximumApplicationAddress;            public uint dwActiveProcessorMask;            public uint dwNumberOfProcessors;            public uint dwProcessorType;            public uint dwAllocationGranularity;            public uint dwProcessorLevel;            public uint dwProcessorRevision;        }         //定义内存的信息结构          [StructLayout(LayoutKind.Sequential)]        public struct MEMORY_INFO        {            public uint dwLength;            public uint dwMemoryLoad;            public uint dwTotalPhys;            public uint dwAvailPhys;            public uint dwTotalPageFile;            public uint dwAvailPageFile;            public uint dwTotalVirtual;            public uint dwAvailVirtual;        }          [DllImport("kernel32",EntryPoint="GetComputerName",ExactSpelling=false,SetLastError=true)]//计算机名称         public static extern bool GetComputerName([MarshalAs(UnmanagedType.LPArray)]byte[] IpBuffer,[MarshalAs(UnmanagedType.LPArray)]Int32[] nSize);        [DllImport("advapi32", EntryPoint = "GetUserName", ExactSpelling = false, SetLastError = true)]//计算机用户名         public static extern bool GetUserName([MarshalAs(UnmanagedType.LPArray)]byte[] IpBuffer, [MarshalAs(UnmanagedType.LPArray)]Int32[] nSize);        [DllImport("Iphlpapi.dll")]        public static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);        [DllImport("Ws2_32.dll")]        public static extern Int32 inet_addr(string ip);       }}

转自:苏飞博客。原文:http://www.cnblogs.com/sufei/archive/2011/06/22/2087363.html

计算机信息类ComputerInfo