苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

分布式系统框架(V1.3) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper万能框架(V2.3) HttpHelper官方出品,无敌框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V1.9) 最牛的爬虫类,没有爬不到只有想不到 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 17277|回复: 19

[访问系统] C#计算机信息类ComputerInfo

[复制链接]
  • TA的每日心情
    开心
    昨天 10:45
  • 签到天数: 641 天

    [LV.9]以坛为家II

    发表于 2012-7-2 11:59:04 | 显示全部楼层 |阅读模式
    导读部分
    -------------------------------------------------------------------------------------------------------------
    C#基类|C#自定义类|C#帮助类--系列导航文章
    http://www.sufeinet.com/thread-655-1-1.html

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

    [C#] 纯文本查看 复制代码
    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
                {
                }
            }
        }
    }

    该用户从未签到

    发表于 2012-8-12 19:38:42 | 显示全部楼层
  • TA的每日心情
    开心
    昨天 10:45
  • 签到天数: 641 天

    [LV.9]以坛为家II

     楼主| 发表于 2012-8-22 09:13:33 | 显示全部楼层
    Jesse 发表于 2012-8-12 11:38
    您这个是不是少了几个class?MAC and WIN32?

    不需要吧

    该用户从未签到

    发表于 2012-8-25 10:25:22 | 显示全部楼层

    该用户从未签到

    发表于 2012-8-25 10:59:38 | 显示全部楼层

    该用户从未签到

    发表于 2012-8-25 15:34:43 | 显示全部楼层
    yangying 发表于 2012-8-25 10:59
    哦,那你去下载工具包吧
    http://www.sufeinet.com/forum.php?mod=viewthread&tid=303

    Thanks very much..

    该用户从未签到

    发表于 2012-11-27 10:42:09 | 显示全部楼层
  • TA的每日心情
    郁闷
    2017-4-6 10:39
  • 签到天数: 1 天

    [LV.1]初来乍到

    发表于 2013-7-18 22:05:56 | 显示全部楼层

    该用户从未签到

    发表于 2014-4-4 00:14:36 | 显示全部楼层

    该用户从未签到

    发表于 2014-5-24 08:30:22 | 显示全部楼层
    您需要登录后才可以回帖 登录 | 马上注册

    本版积分规则

    QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备17001017号-1 )

    GMT+8, 2018-12-11 01:23

    © 2017-2018

    快速回复 返回顶部 返回列表