用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)

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信息是存在/sys/devices/system/cpu中的,在目录中,我们可以看到存在多个文件,一个文件就是一核Cpu的信息。上面写有cpu0,cpu1,cup3诸如此类的文件夹。

要获得它是几核的Cpu,只要读取一下那个cpu目录下有几个cpu+数字的文件夹就可以了。

/

Gets the number of cores available in this device, across all processors

Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu"

@return The number of cores, or 1 if failed to get result

/

private int getNumCores() {

//Private Class to display only CPU devices in the directory listing

class CpuFilter implements FileFilter {

@Override

public boolean accept(File pathname) {

//Check if filename is "cpu", followed by a single digit number

if(Patternmatches("cpu[0-9]", pathnamegetName())) {

return true;

}

return false;

}

}

try {

//Get directory containing CPU info

File dir = new File("/sys/devices/system/cpu/");

//Filter to only list the devices we care about

File[] files = dirlistFiles(new CpuFilter());

//Return the number of cores (virtual CPU devices)

return fileslength;

} catch(Exception e) {

//Default to return 1 core

return 1;

}

}

以上就是关于用c#获取CPU、内存、硬盘、显卡,计算机名如下信息全部的内容,包括:用c#获取CPU、内存、硬盘、显卡,计算机名如下信息、给定一个CPU型号,如何生成对应的cpuid指令注意不是获取是生成 !、android获取手机cpu并判断是单核还是多核等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存