计算机信息类ComputerInfo
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的更多相关文章
- [访问系统] C#计算机信息类ComputerInfo (转载)
下载整个包,只下载现有类是不起作用的 http://www.sufeinet.com/thread-303-1-1.html 点击此处下载 using System; using System.Man ...
- 计算机信息类ComputerInfo(车)
using System; using System.Management; using System.Net; using System.Net.Sockets; using System.Text ...
- [访问系统] Api_Win32_Mac类工具包 (转载)
点击下载 Api_Win32_Mac.zip using System; using System.Collections.Generic; using System.Linq; using Syst ...
- Java类的继承与多态特性-入门笔记
相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...
- 一个实用的却被忽略的命名空间:Microsoft.VisualBasic
当你看到这个命名空间的时候,别因为是VB的东西就匆忙关掉网页,那将会是您的损失,此命名空间中的资源最初目的是为了简化VB.NET开发而创建的,所以Microsoft.VisualBasic并不属于Sy ...
- C# 获取计算机相关信息
整理了一个关于计算机相关系统的资料 需要引入命名空间: 1. 在'解决方案资源管理器' 窗口中->右击项目-> '添加' -> '引用' 弹出引用管理器 2. 在引用处理器中,程序集 ...
- "一个实用的却被忽略的命名空间:Microsoft.VisualBasic":
当你看到这个命名空间的时候,别因为是vb的东西就匆忙关掉网页,那将会是您的损失,此命名空间中的资源最初目的是为了简化vb.net开发而创建的,所以microsoft.visualbasic并不 ...
- [No0000112]ComputerInfo,C#获取计算机信息(cpu使用率,内存占用率,硬盘,网络信息)
github地址:https://github.com/charygao/SmsComputerMonitor 软件用于实时监控当前系统资源等情况,并调用接口,当资源被超额占用时,发送警报到个人手机: ...
- Window系统性能获取帮助类
前言: 这个是获取Windows系统的一些性能的帮助类,其中有:系统内存.硬盘.CPU.网络(个人测试还是比较准的).Ping.单个进程的内存.Cpu.网络(不准). 最初在这个的时候在各种搜索 ...
随机推荐
- Recurrent Neural Networks vs LSTM
Recurrent Neural Network RNN擅长处理序列问题.下面我们就来看看RNN的原理. 可以这样描述:如上图所述,网络的每一个output都会对应一个memory单元用于存储这一时刻 ...
- Java Lambda表达
Java 8 lambda表达式示例 我个人对Java 8发布非常激动,尤其是lambda表达式和流API.越来越多的了解它们,我能写出更干净的代码.虽然一开始并不是这样.第一次看到用lambda表达 ...
- DCU IP Prefether
DCU IP Prefether 数据高速缓存单元预取I P 设置.如果设置为E n a b l e d,会预取I P 地址以改善网络连接和系统性能,所以建议选择E n a b l ed.选项:E n ...
- SFTP无法连接 Connection closed by server with exitcode 127
命令: Pass: ************状态: Connected to 66.77.88.99错误: Connection closed by server with exitcode 127错 ...
- E - Water Distribution
E - Water Distribution 题目大意: 有\(N\)座城市,给定这\(N\)座城市的坐标和初始的水量\(x_i,y_i,a_i\),在两个城市之间运水的花费是两个城市的欧几里得距离. ...
- java基础(5)--流程控制结构
流程控制结构 if结构 当关系表达式为true时,执行语句 if(关系表达式){ //语句块 } if-else结构 当关系表达式为true时,执行语句块1,否则执行语句块2 if(关系表达式){ / ...
- Action<T>和Func<T>
Action<T>和Func<T>都是泛型委托. Action<T>表示委托可以引用一个viod返回类型的方法,至于方法是带几个参数,什么类型的参数,由后面的泛型决 ...
- Hibernate -- 项目结构模型改造, 加 Utils 和 Dao层
示例代码: App.java 模拟客户端 /** * 模拟客户端 */ public class App { @Test public void saveCustomer(){ CustomerDao ...
- InfiniBand技术和协议架构分析
Infiniband开放标准技术简化并加速了服务器之间的连接,同时支持服务器与远程存储和网络设备的连接. IB技术的发展 1999年开始起草规格及标准规范,2000年正式发表,但发展速度不及Rapid ...
- Shell 自定义函数
语法: function fname() { 程序段} 例子: #!/bin/bash ## 定义函数,分子除以分母,算利润.占有率等## 参数1:分子## 参数2:分母 function divfu ...