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. Linux负载均衡--LVS(IPVS)

    一.LVS简介 LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器, 是一个由章文嵩博士发起的自由软件项目,现在已经是 Linux标准内核的一部分.LVS是一种叫基于T ...

  2. Python3.x:Linux下安装python3.6

    Python3.x:Linux下安装python3.6 下载 #先进入download文件夹 cd /home/download #输入命令(下载到当前目录) wget https://www.pyt ...

  3. Django QuerySet API

    https://docs.djangoproject.com/en/2.1/ref/models/querysets/

  4. m2eclipse插件——添加依赖不显示搜索结果

    使用Eclipse,安装m2eclipse插件之后,选中Maven项目的pom文件,添加依赖,点击“Add Dependency”的时候,输入要检索的jar包名称,search result却一直为空 ...

  5. JMeter学习(二)工具简单介绍

    一.JMeter 介绍 Apache JMeter是100%纯JAVA桌面应用程序,被设计为用于测试客户端/服务端结构的软件(例如web应用程序).它可以用来测试静态和动态资源的性能,例如:静态文件, ...

  6. 混合开发的大趋势之 一个Android程序员眼中的 React.js 箭头函数,const, PropTypes

    转载请注明出处:王亟亟的大牛之路 昨天写了篇React.js的开头之作,讲了讲块级作用域和let,先安利:https://github.com/ddwhan0123/Useful-Open-Sourc ...

  7. 图论_FatherChristmasFlymouse(Tarjan+dijkstra or spfa)

    堆优化Dij VS Spfa 堆优化Dij小胜一筹. 题目名字:Father Christmas flymouse (POJ 3160) 这题可以说是图论做的比较畅快的一题,比较综合,很想说一说. 首 ...

  8. C++转C#函数事例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...

  9. dataframe 列名重新排序

    在用list包含多个dict的模式生成dataframe时,由于dict的无序性,而uci很多数据的特征名直接是1,2,3...,生成的dataframe和原生的不一样, 为了方便观看和使用,我们将其 ...

  10. QSS设置字体不起作用

    正确的QSS QLabel{ font-family: "Microsoft YaHei"; font-weight:bold; font-size:14px; color: #3 ...