http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 30937|回复: 19

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

[复制链接]
发表于 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
            {
            }
        }
    }
}


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2012-8-12 19:38:42 | 显示全部楼层
您这个是不是少了几个class?MAC and WIN32?
 楼主| 发表于 2012-8-22 09:13:33 | 显示全部楼层
Jesse 发表于 2012-8-12 11:38
您这个是不是少了几个class?MAC and WIN32?

不需要吧
发表于 2012-8-25 10:25:22 | 显示全部楼层
admin 发表于 2012-8-22 09:13
不需要吧

但是我这儿确实报错了。
QQ截图20120825102223.png
发表于 2012-8-25 10:59:38 | 显示全部楼层
Jesse 发表于 2012-8-25 10:25
但是我这儿确实报错了。

哦,那你去下载工具包吧
http://www.sufeinet.com/thread-303-1-1.html
发表于 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 | 显示全部楼层
真心感谢。。
发表于 2013-7-18 22:05:56 | 显示全部楼层
真是难得给力的帖子啊,强烈支持楼主。
发表于 2014-4-4 00:14:36 | 显示全部楼层
受教了,学习中……
发表于 2014-5-24 08:30:22 | 显示全部楼层
受教了,学习中……
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2024-11-25 06:52

© 2014-2021

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