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
{
}
}
}
}

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); }
}

计算机信息类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入门笔记] 面向对象编程基础(一):类和对象

    什么是面向对象编程? 我们先来看看几个概念: 面向过程程序设计 面向过程,是根据事情发展的步骤,按进行的顺序过程划分,面向过程其实是最为实际的一种思考方式,可以说面向过程是一种基础的方法,它考虑的是实 ...

  5. OC第一节 —— 类和对象

    一.类和对象的概念 1.1类 自己的定义: 具有相同或相似性质对象的抽象. 1.2 对象 自己的定义: 对象是人们要进行研究的任何物体,从最简单的整数到复杂的飞机 等均可以看做是对象. 举例说明: 类 ...

  6. python的类和对象——进阶篇

    写在前面的话 终于,又到了周五.当小伙伴们都不再加班欢欢喜喜过周末的时候,我刚刚写完这一周的游戏作业,从面对晚归的紧皱眉头到现在的从容淡定,好像只有那么几周的时间.突然发现:改变——原来这么简单.很多 ...

  7. python第三十九课——面向对象(二)之设计类

    1.设计类class 车: #属性 颜色 = red 品牌 = "BMW" 车牌 = "沪A88888" #函数 行驶(): 停止(): 2.实例化车对象 ca ...

  8. 我的Java之旅——答答租车系统的改进

    之前的答答租车系统虽然可以实现项目的要求,但是没有用Java面向对象,今天用面向对象的三大特性封装.继承和多态来改进原来的代码.题目和之前的代码参考上篇博客,这里不再述说. 改进后的代码: Vehic ...

  9. 第7.11节 案例详解:Python类实例变量

    上节老猿介绍了实例变量的访问方法,本节结合一个具体案例详细介绍实例变量访问. 本节定义一个Vehicle类(车),它有三个实例变量self.wheelcount(轮子数).self.power(动力) ...

随机推荐

  1. request和reponse

  2. python基础 range()与np.arange()

    range()返回的是range object,而np.nrange()返回的是numpy.ndarray() range尽可用于迭代,而np.nrange作用远不止于此,它是一个序列,可被当做向量使 ...

  3. C++ 模态与非模态对话框

    视频教程:模态与非模态对话框1 模态对话框:子窗口关闭之前,不能对父窗口操作 非模态对话框:子窗口关闭之前,可以对父窗口操作 插入一个对话框: 资源视图--->右击---> 进行类的绑定: ...

  4. Linux之Ubuntu下DSL拨号上网

    可视化桌面配置方法 1.编辑连接 2.选择 增加 3.选择 DSL 4.选择 新建连接[cmcc@gx属于移动校园用户的ISP指定后缀] 6.OK 当然,还有其他拨号上网的办法: [Linux/Ubu ...

  5. mac 下node,yarn安装及版本切换

    node安装 https://nodejs.org/en/download/ 到官网下载指定版本 安装node的管理工具 sudo npm install -g n //安装n sudo n 8.9. ...

  6. Codeforces 1065E(计数)

    题目链接 题意 限定字符串长度为$n$,字符集规模为$A$,以及$m$个数字$b$,对于任意数字$bi$满足长度为$bi$的前缀和后缀先反转再交换位置后形成的新串与原串视作相等,问存在多少不同串. 思 ...

  7. EcustOJ P109跳一跳(离散化+dp)

    题目链接 感觉这道题我看了很多天,胡思乱想啊,一开始觉得记忆化搜索会可能T啊,,可能出题人的数据卡的好就稳T了的感觉..后来想了想,好像离散化一下,记一下位置之后再记忆化搜索就应该稳了吧..(好像直接 ...

  8. Spark的四种部署方式并对应四种提交方式

    1 Local模式     本地模式  local模式 一台机器即可,开箱即用 不指定master,或者 --master  local  local[N]  local[*] local模式下,使用 ...

  9. 5.22 HTML 列表标签和表单标签

    1,ul无序列表 标签 ul:unordered list ,就是无序列表的意思. li:  listitem  列表项的意思.无序列表的每一项都是<li>. <!DOCTYPE h ...

  10. 解释局域(LAN)和广域网(WAN)之间的区别,它们之间的关系是什么?

    解释局域(LAN)和广域网(WAN)之间的区别,它们之间的关系是什么?