用c#获取CPU、内存、硬盘、显卡,计算机名如下信息

用c#获取CPU、内存、硬盘、显卡,计算机名如下信息,第1张

代码如下(具体看注释):

view plaincopy to clipboardprint

using System;

using SystemManagement;

namespace SoyeeComm

{

/// summary

/// Computer Information

/// /summary

public class Computer

{

public string CpuID;

public string MacAddress;

public string DiskID;

public string IpAddress;

public string LoginUserName;

public string ComputerName;

public string SystemType;

public string TotalPhysicalMemory; //单位:M

private static Computer _instance;

public static Computer Instance()

{

if (_instance == null)

_instance = new Computer();

return _instance;

}

protected Computer()

{

CpuID=GetCpuID();

MacAddress=GetMacAddress();

DiskID=GetDiskID();

IpAddress=GetIPAddress();

LoginUserName=GetUserName();

SystemType=GetSystemType();

TotalPhysicalMemory=GetTotalPhysicalMemory();

ComputerName=GetComputerName();

}

string GetCpuID()

{

try

{

//获取CPU序列号代码

string cpuInfo = ;//cpu序列号

ManagementClass mc = new ManagementClass(Win32_Processor);

ManagementObjectCollection moc = mcGetInstances();

foreach(ManagementObject mo in moc)

{

cpuInfo = moProperties[ProcessorId]ValueToString();

}

moc=null;

mc=null;

return cpuInfo;

}

catch

{

return unknow;

}

finally

{

}

}

string GetMacAddress()

{

try

{

//获取网卡硬件地址

string mac=;

ManagementClass mc = new ManagementClass(Win32_NetworkAdapterConfiguration);

ManagementObjectCollection moc = mcGetInstances();

foreach(ManagementObject mo in moc)

{

if((bool)mo[IPEnabled] == true)

{

mac=mo[MacAddress]ToString();

break;

}

}

moc=null;

mc=null;

return mac;

}

catch

{

return unknow;

}

finally

{

}

}

string GetIPAddress()

{

try

{

//获取IP地址

string st=;

ManagementClass mc = new ManagementClass(Win32_NetworkAdapterConfiguration);

ManagementObjectCollection moc = mcGetInstances();

foreach(ManagementObject mo in moc)

{

if((bool)mo[IPEnabled] == true)

{

//st=mo[IpAddress]ToString();

SystemArray ar;

ar=(SystemArray)(moProperties[IpAddress]Value);

st=arGetValue(0)ToString();

break;

}

}

moc=null;

mc=null;

return st;

}

catch

{

return unknow;

}

finally

{

}

}

string GetDiskID()

{

try

{

//获取硬盘ID

String HDid=;

ManagementClass mc = new ManagementClass(Win32_DiskDrive);

ManagementObjectCollection moc = mcGetInstances();

foreach(ManagementObject mo in moc)

{

HDid = (string)moProperties[Model]Value;

}

moc=null;

mc=null;

return HDid;

}

catch

{

return unknow;

}

finally

{

}

}

/// summary

/// *** 作系统的登录用户名

/// /summary

/// returns/returns

string GetUserName()

{

try

{

string st=;

ManagementClass mc = new ManagementClass(Win32_ComputerSystem);

ManagementObjectCollection moc = mcGetInstances();

foreach(ManagementObject mo in moc)

{

st=mo[UserName]ToString();

}

moc=null;

mc=null;

return st;

}

catch

{

return unknow;

}

finally

{

}

}

/// summary

/// PC类型

/// /summary

/// returns/returns

string GetSystemType()

{

try

{

string st=;

ManagementClass mc = new ManagementClass(Win32_ComputerSystem);

ManagementObjectCollection moc = mcGetInstances();

foreach(ManagementObject mo in moc)

{

st=mo[SystemType]ToString();

}

moc=null;

mc=null;

return st;

}

catch

{

return unknow;

}

finally

{

}

}

/// summary

/// 物理内存

/// /summary

/// returns/returns

string GetTotalPhysicalMemory()

{

try

{

string st=;

ManagementClass mc = new ManagementClass(Win32_ComputerSystem);

ManagementObjectCollection moc = mcGetInstances();

foreach(ManagementObject mo in moc)

方法大体有(一)(二)两种:

(一)学会看主板的cpu插槽,以及cpu的插槽类型,两者相同,就支持。这些参数都可以在百度搜索。如果觉得看型号复杂,没关系,往下看。

(二)在主板官网搜索主板型号(主板型号可以通过鲁大师、aida64等软件查看),然后搜索结果会显示这个型号所支持的cpu。以技嘉B75M D3V主板为例:

百度搜索“技嘉主板”,点击进入红色箭头所指,如下图:

在技嘉主板官网右上角输入主板型号“B75M D3V”,如下图:

搜索结果靠下,往下拉就看到了,不算难找,点击它,如下图:

点击“产品规格”选项卡,如下图箭头②,即可看到这块主板所支持的cpu的列表了:

CPUID指令是intel IA32架构下获得CPU信息的汇编指令,可以得到CPU类型,型号,制造商信息,商标信息,序列号,缓存等一系列CPU相关的东西。

比如生成查询CPU的制造商信息(Vender ID String)的代码:

string CPUID::GetVID()

{

char cVID[13]; // 字符串,用来存储制造商信息

memset(cVID, 0, 13); // 把数组清0

Executecpuid(0); // 执行cpuid指令,使用输入参数 eax = 0

memcpy(cVID, &m_ebx, 4); // 复制前四个字符到数组

memcpy(cVID+4, &m_edx, 4); // 复制中间四个字符到数组

memcpy(cVID+8, &m_ecx, 4); // 复制最后四个字符到数组

return string(cVID); // 以string的形式返回

}

Android获取cpu和内存信息、网址的代码如下:

/ 获取用户硬件信息 /

public static String getMobileInfo() {

//StringBuffer sb = new StringBuffer();

JSONObject mbInfo = new JSONObject();

//通过反射获取用户硬件信息

try {

Field[] fields = BuildclassgetDeclaredFields();

for (Field field : fields) {

// 暴力反射,获取私有信息

fieldsetAccessible(true);

String name = fieldgetName();

String value = fieldget(null)toString();

//sbappend(name + "=" + value);

//sbappend("\n");

mbInfoput(name, value);

}

} catch (Exception e) {

eprintStackTrace();

}

//return sbtoString();

return mbInfotoString();

}

static public String getCpuString(){

if(BuildCPU_ABIequalsIgnoreCase("x86")){

return "Intel";

}

String strInfo = "";

try

{

byte[] bs = new byte[1024];

RandomAccessFile reader = new RandomAccessFile("/proc/cpuinfo", "r");

readerread(bs);

String ret = new String(bs);

int index = retindexOf(0);

if(index != -1) {

strInfo = retsubstring(0, index);

} else {

strInfo = ret;

}

}

catch (IOException ex){

exprintStackTrace();

}

return strInfo;

}

static public String getCpuType(){

String strInfo = getCpuString();

String strType = null;

if (strInfocontains("ARMv5")) {

strType = "armv5";

} else if (strInfocontains("ARMv6")) {

strType = "armv6";

} else if (strInfocontains("ARMv7")) {

strType = "armv7";

} else if (strInfocontains("Intel")){

strType = "x86";

}else{

strType = "unknown";

return strType;

}

if (strInfocontains("neon")) {

strType += "_neon";

}else if (strInfocontains("vfpv3")) {

strType += "_vfpv3";

}else if (strInfocontains(" vfp")) {

strType += "_vfp";

}else{

strType += "_none";

}

return strType;

}

/

@hide

@return

/

public static CPUInfo getCPUInfo() {

String strInfo = null;

try

{

byte[] bs = new byte[1024];

RandomAccessFile reader = new RandomAccessFile("/proc/cpuinfo", "r");

readerread(bs);

String ret = new String(bs);

int index = retindexOf(0);

if(index != -1) {

strInfo = retsubstring(0, index);

} else {

strInfo = ret;

}

}

catch (IOException ex)

{

strInfo = "";

exprintStackTrace();

}

CPUInfo info = parseCPUInfo(strInfo);

infomCPUMaxFreq = getMaxCpuFreq();

return info;

}

private final static String kCpuInfoMaxFreqFilePath = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";

private static int getMaxCpuFreq() {

int result = 0;

FileReader fr = null;

BufferedReader br = null;

try {

fr = new FileReader(kCpuInfoMaxFreqFilePath);

br = new BufferedReader(fr);

String text = brreadLine();

if (text != null) {

result = IntegerparseInt(texttrim());

}

} catch (FileNotFoundException e) {

eprintStackTrace();

} catch (IOException e) {

eprintStackTrace();

} finally {

if (fr != null)

try {

frclose();

} catch (IOException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

if (br != null)

try {

brclose();

} catch (IOException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

}

return result;

}

public static class CPUInfo{

public CPUInfo(){

}

public static final int CPU_TYPE_UNKNOWN = 0x00000000;

public static final int CPU_TYPE_ARMV5TE = 0x00000001;

public static final int CPU_TYPE_ARMV6 = 0x00000010;

public static final int CPU_TYPE_ARMV7 = 0x00000100;

public static final int CPU_FEATURE_UNKNOWS = 0x00000000;

public static final int CPU_FEATURE_VFP = 0x00000001;

public static final int CPU_FEATURE_VFPV3 = 0x00000010;

public static final int CPU_FEATURE_NEON = 0x00000100;

public int mCPUType;

public int mCPUCount;

public int mCPUFeature;

public double mBogoMips;

public long mCPUMaxFreq;

}

/

@param cpuInfo

@return

@hide

/

private static CPUInfo parseCPUInfo(String cpuInfo) {

if (cpuInfo == null || ""equals(cpuInfo)) {

return null;

}

CPUInfo ci = new CPUInfo();

cimCPUType = CPUInfoCPU_TYPE_UNKNOWN;

cimCPUFeature = CPUInfoCPU_FEATURE_UNKNOWS;

cimCPUCount = 1;

cimBogoMips = 0;

if (cpuInfocontains("ARMv5")) {

cimCPUType = CPUInfoCPU_TYPE_ARMV5TE;

} else if (cpuInfocontains("ARMv6")) {

cimCPUType = CPUInfoCPU_TYPE_ARMV6;

} else if (cpuInfocontains("ARMv7")) {

cimCPUType = CPUInfoCPU_TYPE_ARMV7;

}

if (cpuInfocontains("neon")) {

cimCPUFeature |= CPUInfoCPU_FEATURE_NEON;

}

if (cpuInfocontains("vfpv3")) {

cimCPUFeature |= CPUInfoCPU_FEATURE_VFPV3;

}

if (cpuInfocontains(" vfp")) {

cimCPUFeature |= CPUInfoCPU_FEATURE_VFP;

}

String[] items = cpuInfosplit("\n");

for (String item : items) {

if (itemcontains("CPU variant")) {

int index = itemindexOf(": ");

if (index >= 0) {

String value = itemsubstring(index + 2);

try {

cimCPUCount = Integerdecode(value);

cimCPUCount = cimCPUCount == 0 1 : cimCPUCount;

} catch (NumberFormatException e) {

cimCPUCount = 1;

}

}

} else if (itemcontains("BogoMIPS")) {

int index = itemindexOf(": ");

if (index >= 0) {

String value = itemsubstring(index + 2);

}

}

}

return ci;

}

/

获取设备内存大小值

@return 内存大小,单位MB

/

public static long getTotalMemory() {

String str1 = "/proc/meminfo";

String str2;

String[] arrayOfString;

long initial_memory = 0;

try {

FileReader localFileReader = new FileReader(str1);

BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);

str2 = localBufferedReaderreadLine();

if (str2 != null) {

arrayOfString = str2split("\\s+");

initial_memory = IntegervalueOf(arrayOfString[1])intValue()/1024;

}

localBufferedReaderclose();

return initial_memory;

}

catch (IOException e)

{

return -1;

}

}

/

@hide

@return

/

public CPUInfo getCPUInfo() {

String strInfo = null;

try

{

byte[] bs = new byte[1024];

RandomAccessFile reader = new RandomAccessFile("/proc/cpuinfo", "r");

readerread(bs);

String ret = new String(bs);

int index = retindexOf(0);

if(index != -1) {

strInfo = retsubstring(0, index);

} else {

strInfo = ret;

}

}

catch (IOException ex)

{

strInfo = "";

exprintStackTrace();

}

CPUInfo info = parseCPUInfo(strInfo);

infomCPUMaxFreq = getMaxCpuFreq();

return info;

}

/

获取android CPU类型

@return String CPU类型

/

public static String getCpuModel(){

String cpu_model = "";

CPUInfo in = getCPUInfo();

if ((inmCPUType & CPUInfoCPU_TYPE_ARMV5TE) == CPUInfoCPU_TYPE_ARMV5TE)

cpu_model="armv5";

else if ((inmCPUType & CPUInfoCPU_TYPE_ARMV6) == CPUInfoCPU_TYPE_ARMV6)

cpu_model="armv6";

else if ((inmCPUType & CPUInfoCPU_TYPE_ARMV7) == CPUInfoCPU_TYPE_ARMV7)

cpu_model="armv7";

else

cpu_model="unknown";

return cpu_model;

}

/

获取android CPU特性

@return String CPU特性

/

public static String getCpuFeature(){

String cpu_feature = "";

CPUInfo in = getCPUInfo();

if ((inmCPUFeature & CPUInfoCPU_FEATURE_NEON ) == CPUInfoCPU_FEATURE_NEON)

cpu_feature="neon";

else if ((inmCPUFeature & CPUInfoCPU_FEATURE_VFP ) == CPUInfoCPU_FEATURE_VFP)

cpu_feature="vfp";

else if ((inmCPUFeature & CPUInfoCPU_FEATURE_VFPV3 ) == CPUInfoCPU_FEATURE_VFPV3)

cpu_feature="vfpv3";

else

cpu_feature="unknown";

return cpu_feature;

}

/

获取ip地址

@param mContext Context

@return ip地址字符串

/

public static String getIpAddress(Context mContext) {

String ipAddress = null;

try {

for (Enumeration en = NetworkInterface

getNetworkInterfaces(); enhasMoreElements();) {

NetworkInterface intf = ennextElement();

for (Enumeration enumIpAddr = intf

getInetAddresses(); enumIpAddrhasMoreElements();) {

InetAddress inetAddress = enumIpAddrnextElement();

if (!inetAddressisLoopbackAddress()) {

ipAddress = inetAddressgetHostAddress()toString();

}

}

}

} catch (SocketException ex) {

return null;

}

if (DEBUG) {

Logd(TAG, "ip address:" + ipAddress);

}

return ipAddress;

}

以上就是关于用c#获取CPU、内存、硬盘、显卡,计算机名如下信息全部的内容,包括:用c#获取CPU、内存、硬盘、显卡,计算机名如下信息、怎么看电脑主板支持什么样的CPU、给定一个CPU型号,如何生成对应的cpuid指令注意不是获取是生成 !等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9276473.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存