• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[访问系统]C#计算机信息类ComputerInfo(转载)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

下载整个包,只下载现有类是不起作用的 http://www.sufeinet.com/thread-303-1-1.html

点击此处下载

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 = 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
            {
            }
        }
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
【转】【C#】C#5.0新特性——Async和Await使异步编程更简单发布时间:2022-07-14
下一篇:
C#判断一个string是否为数字--zt发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap