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 = ;
int res = API.SendARP(idest, , ref macInfo, ref length);
string mac_src = macInfo.ToString("X");
if (!string.IsNullOrEmpty(mac_src) && !"".Equals(mac_src))
{
while (mac_src.Length < )
{
mac_src = mac_src.Insert(, "");
}
string mac_dest = string.Empty;
for (int i = ; i < ; i++)
{
if (i % == )
{
if (i == )
mac_dest = mac_dest.Insert(, mac_src.Substring(i, ));
else
mac_dest = "-" + mac_dest.Insert(, mac_src.Substring(i, ));
}
}
return mac_dest;
}
}
catch
{
return "";
}
return "";
} /// <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[];
Int32[] length = new Int32[];
length[] = ;//限定用户名长度
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[];
Int32[] length = new Int32[];
length[] = ;//限定计算机名长度
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的更多相关文章

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

    下载整个包,只下载现有类是不起作用的 http://www.sufeinet.com/thread-303-1-1.html 点击此处下载 using System; using System.Man ...

  2. 计算机信息类ComputerInfo(车)

    using System; using System.Management; using System.Net; using System.Net.Sockets; using System.Text ...

  3. [访问系统] Api_Win32_Mac类工具包 (转载)

    点击下载 Api_Win32_Mac.zip using System; using System.Collections.Generic; using System.Linq; using Syst ...

  4. Java类的继承与多态特性-入门笔记

    相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...

  5. 一个实用的却被忽略的命名空间:Microsoft.VisualBasic

    当你看到这个命名空间的时候,别因为是VB的东西就匆忙关掉网页,那将会是您的损失,此命名空间中的资源最初目的是为了简化VB.NET开发而创建的,所以Microsoft.VisualBasic并不属于Sy ...

  6. C# 获取计算机相关信息

    整理了一个关于计算机相关系统的资料 需要引入命名空间: 1. 在'解决方案资源管理器' 窗口中->右击项目-> '添加' -> '引用' 弹出引用管理器 2. 在引用处理器中,程序集 ...

  7. "一个实用的却被忽略的命名空间:Microsoft.VisualBasic":

        当你看到这个命名空间的时候,别因为是vb的东西就匆忙关掉网页,那将会是您的损失,此命名空间中的资源最初目的是为了简化vb.net开发而创建的,所以microsoft.visualbasic并不 ...

  8. [No0000112]ComputerInfo,C#获取计算机信息(cpu使用率,内存占用率,硬盘,网络信息)

    github地址:https://github.com/charygao/SmsComputerMonitor 软件用于实时监控当前系统资源等情况,并调用接口,当资源被超额占用时,发送警报到个人手机: ...

  9. Window系统性能获取帮助类

    前言: 这个是获取Windows系统的一些性能的帮助类,其中有:系统内存.硬盘.CPU.网络(个人测试还是比较准的).Ping.单个进程的内存.Cpu.网络(不准).    最初在这个的时候在各种搜索 ...

随机推荐

  1. 通过自动回复机器人学Mybatis:代码重构(分层)

    imooc视频学习笔记 ----> URL:http://www.imooc.com/learn/154 ListServlet.java package com.imooc.servlet; ...

  2. 【Java Web】新手教程(转)

    转自:http://www.journaldev.com/1854/java-web-application-tutorial-for-beginners#web-server-client Web ...

  3. SpringMVC的RequestMapping

    在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值,以及在 ...

  4. 使用POI做的一个生成Excel的工具类。包含了导出Excel和解析Excel方法

    PoiExcelUtils.java /** * */ package com.common.office; import java.io.File; import java.io.FileInput ...

  5. MySQL-checkpoint技术

    几个知识点: 缓冲池:缓存磁盘数据,通过内存速度弥补CPU速度和磁盘速度的鸿沟. 脏页:LRU列表中被修改的页,和磁盘上的数据不一致 刷新频率:每次有脏页就刷新,开销很大.需要一种刷新机制 数据丢失: ...

  6. C++线程池总结

    本文采用pthread实现线程池,有以下几个类. CTask:任务抽象类,主要提供接口,供子类实现. CMyTask:继承CTask实现接口 CThreadPool:线程池类,用于管理线程. 信号量: ...

  7. C++中虚函数和纯虚函数的区别与总结

    首先:强调一个概念 定义一个函数为虚函数,不代表函数为不被实现的函数. 定义他为虚函数是为了允许用基类的指针来调用子类的这个函数. 定义一个函数为纯虚函数,才代表函数没有被实现. 定义纯虚函数是为了实 ...

  8. eclipse——反编译插件

    百度云链接 链接:https://pan.baidu.com/s/1iEtstiK5mJ4kDp6gTfVBww 密码:8wf7 在线安装地址 http://jd.benow.ca/jd-eclips ...

  9. Apache顶级项目 Calcite使用介绍

    什么是Calcite Apache Calcite是一个动态数据管理框架,它具备很多典型数据库管理系统的功能,比如SQL解析.SQL校验.SQL查询优化.SQL生成以及数据连接查询等,但是又省略了一些 ...

  10. Google推荐——Glide使用详解(图片加载框架)

    零.前言 本文所使用的Glide版本为3.7.0 一.简介 Glide,一个被google所推荐的图片加载库,作者是bumptech.这个库被广泛运用在google的开源项目中,包括2014年的goo ...