C# 获取计算机信息

目录

一、本机信息

1、本机名

2、获得本机MAC地址

3、获得计算机名

4、显示器分辨率

5、主显示器分辨率

6、系统路径

二、操作系统信息

1、操作系统类型

2、获得操作系统位数

3、获得操作系统版本

三、处理器信息

[1 、处理器个数](#1 、处理器个数)

四、CPU信息

1、CPU的个数

2、CPU版本信息

3、CPU名称信息

4、CPU序列号

5、CPU制造厂商

6、CPU频率

五、显卡信息

1、显卡PNPDeviceID

六、硬盘信息

1、设备硬盘的卷标号

2、硬盘的大小

3、硬盘序列号

七、主板信息

1、主板序列号

2、主板制造厂商

3、主板编号

4、主板型号

八、内存信息

1、物理内存大小

2、内存利用率

九、网卡信息

1、网卡硬件地址

2、网卡地址

3、适配器信息

十、声卡信息

1、声卡PNPDeviceID

十一、其他信息

1、鼠标按钮个数

十二、网络信息

1、网络连接

2、IP地址


一、本机信息

1、本机名

 public static string getLocalhostName(){
    return Environment.GetEnvironmentVariable("COMPUTERNAME");
 }

2、获得本机MAC地址

public static string getLocalhostMac() {
    string mac = null;
    ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection queryCollection = query.Get();
    foreach (ManagementObject mo in queryCollection) {
		if (mo["IPEnabled"].ToString() == "True")
		mac = mo["MacAddress"].ToString();
    }
    return (mac);
}

3、获得计算机名

public static string getComputerName() {
    return Environment.MachineName;
}

4、显示器分辨率

public static string getResolution() {
    return SystemInformation.PrimaryMonitorMaximizedWindowSize.Width + " x " + SystemInformation.PrimaryMonitorMaximizedWindowSize.Height;
}

5、主显示器分辨率

public static string getMainResolution() {
    return SystemInformation.PrimaryMonitorSize.Width + " x " + SystemInformation.PrimaryMonitorSize.Height;
}

6、系统路径

[DllImport("kernel32")]
public static extern void GetSystemDirectory(StringBuilder SysDir, int count);

public static string getSystemPath() {
    const int nChars = 128;
    StringBuilder Buff = new StringBuilder(nChars);
    GetSystemDirectory(Buff, nChars);
    return Buff.ToString();
}

二、操作系统信息

1、操作系统类型

public static string getSystemType() {
    string st = "";
    ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
    ManagementObjectCollection moc = mc.GetInstances();
    foreach (ManagementObject mo in moc) {
		st = mo["SystemType"].ToString();
    }
    return st;
}

2、获得操作系统位数

public static string getOSBit() {
    if (Environment.Is64BitOperatingSystem) {
		return "64bit."
    } else {
		return  "32bit.";
    }
}

3、获得操作系统版本

public static string getOSVersion()  {
    return Environment.OSVersion.VersionString;
}

三、处理器信息

1 、处理器个数

public static string getProcessorCount()  {
    return Environment.ProcessorCount.ToString();
}

四、CPU信息

1、CPU的个数

public static int getCpuCount() {
    try {
		using (ManagementClass mCpu = new ManagementClass("Win32_Processor")) {
			ManagementObjectCollection cpus = mCpu.GetInstances();
			return cpus.Count;
		}
    } catch { }
    return -1;
}

2、CPU版本信息

public static string getCpuVersion() {
    string st = "";
    ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_Processor");
    foreach (ManagementObject mo in mos.Get()) {
		st = mo["Version"].ToString();
    }
    return st;
}

3、CPU名称信息

public static string getCpuName() {
    string st = "";
    ManagementObjectSearcher driveID = new ManagementObjectSearcher("Select * from Win32_Processor");
    foreach (ManagementObject mo in driveID.Get()) {
		st = mo["Name"].ToString();
    }
    return st;
}

4、CPU序列号

public static string getCpuSerialNumber() {
    string strCpu = null;
    ManagementClass myCpu = new ManagementClass("win32_Processor");
    ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
    foreach (ManagementObject myObject in myCpuConnection) {
		strCpu = myObject.Properties["Processorid"].Value.ToString();
		break;
    }
    return strCpu;
}

5、CPU制造厂商

public static string getCPUManufacturer() {
    string st = "";
    ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_Processor");
    foreach (ManagementObject mo in mos.Get()) {
		st = mo["Manufacturer"].ToString();
    }
    return st;
}

6、CPU频率

这里之所以使用string类型的数组,主要是因为cpu的多核

public static string[] getCpuMHZ() {
    ManagementClass mc = new ManagementClass("Win32_Processor");
    ManagementObjectCollection cpus = mc.GetInstances(); 
    string[] mHz = new string[cpus.Count];
    int c = 0;
    ManagementObjectSearcher mySearch = new ManagementObjectSearcher("select * from Win32_Processor");
    foreach (ManagementObject mo in mySearch.Get()) {
		mHz[c] = mo.Properties["CurrentClockSpeed"].Value.ToString();
		c++;
    }
    mc.Dispose();
    mySearch.Dispose();
    return mHz;
}

五、显卡信息

1、显卡PNPDeviceID

public static string getVideoPNPID() {
    string st = "";
    ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_VideoController");
    foreach (ManagementObject mo in mos.Get()) {
		st = mo["PNPDeviceID"].ToString();
    }
    return st;
}

六、硬盘信息

1、设备硬盘的卷标号

public static string getDiskVolumeSerialNumber() {
    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
    disk.Get();
    return disk.GetPropertyValue("VolumeSerialNumber").ToString();
}

2、硬盘的大小

public static string getSizeOfDisk() {
    ManagementClass mc = new ManagementClass("Win32_DiskDrive");
    ManagementObjectCollection moj = mc.GetInstances();
    foreach (ManagementObject m in moj) {
		return m.Properties["Size"].Value.ToString();
    }
    return "-1";
}

3、硬盘序列号

public static string getHardDiskSerialNumber() {
    try {
		ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
		string sHardDiskSerialNumber = "";
		foreach (ManagementObject mo in searcher.Get()) {
			sHardDiskSerialNumber = mo["SerialNumber"].ToString().Trim();
			break;
		}
		return sHardDiskSerialNumber;
    }
    catch {
		return "";
    }
}

七、主板信息

1、主板序列号

public static string getBIOSSerialNumber() {
    try {
		ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_BIOS");
		string sBIOSSerialNumber = "";
		foreach (ManagementObject mo in searcher.Get()) {
			sBIOSSerialNumber = mo["SerialNumber"].ToString().Trim();
		}
		return sBIOSSerialNumber;
    }
    catch {
		return "";
    }
}

2、主板制造厂商

public static string getBoardManufacturer() {
    SelectQuery query = new SelectQuery("Select * from Win32_BaseBoard");
    ManagementObjectSearcher mos = new ManagementObjectSearcher(query);
    ManagementObjectCollection.ManagementObjectEnumerator data = mos.Get().GetEnumerator();
    data.MoveNext();
    ManagementBaseObject board = data.Current;
    return board.GetPropertyValue("Manufacturer").ToString();
}

3、主板编号

public static string getBoardID() {
    string st = "";
    ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
    foreach (ManagementObject mo in mos.Get()) {
		st = mo["SerialNumber"].ToString();
    }
    return st;
}

4、主板型号

public static string getBoardType() {
    string st = "";
    ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
    foreach (ManagementObject mo in mos.Get()) {
		st = mo["Product"].ToString();
    }
    return st;
}

八、内存信息

1、物理内存大小

public static string getPhysicalMemory() {
    string st = "";
    ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
    ManagementObjectCollection moc = mc.GetInstances();
    foreach (ManagementObject mo in moc) {
		st = mo["TotalPhysicalMemory"].ToString();
    }
    return st;
}

2、内存利用率

[DllImport("kernel32")]
private static extern void GlobalMemoryStatus(ref StorageInfo memibfo);

[StructLayout(LayoutKind.Sequential)]
public struct StorageInfo //此处全是以字节为单位 {
    public uint dwMemoryLoad;//内存使用率
}

public static string getUtilizationRate() {
    StorageInfo memInfor = new StorageInfo();
    GlobalMemoryStatus(ref memInfor);
    return memInfor.dwMemoryLoad.ToString("0.0");
}

九、网卡信息

1、网卡硬件地址

public static string getMacAddress() {
    string mac = "";
    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection moc = mc.GetInstances();
    foreach (ManagementObject mo in moc) {
		if ((bool)mo["IPEnabled"] == true) {
			mac = mo["MacAddress"].ToString();
			break;
		}
    }
    return mac;
}

2、网卡地址

public static string getNetCardMACAddress() {
    try {
		ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL) AND (Manufacturer <> 'Microsoft'))");
		string NetCardMACAddress = "";
		foreach (ManagementObject mo in searcher.Get()) {
			NetCardMACAddress = mo["MACAddress"].ToString().Trim();
		}
		return NetCardMACAddress;
    }
    catch {
		return "";
    }
}

3、适配器信息

public static NetworkInterface[] getNetWorkInfo() {
    NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
    return adapters;
}
// 设置内容向文本框
public void setContent() {
    tb_net_work_adapter_num.Text = adapters.Length.ToString();
    int index = 0;
    foreach (NetworkInterface adapter in adapters) {
		index++;
		//显示网络适配器描述信息、名称、类型、速度、MAC 地址
		tb_net_wark_adapter_info.Text = tb_net_wark_adapter_info.Text + ("---------------------第" + index + "个适配器信息---------------------") + "\n";
		tb_net_wark_adapter_info.Text = tb_net_wark_adapter_info.Text + ("描述信息:" + adapter.Name) + "\n";
		tb_net_wark_adapter_info.Text = tb_net_wark_adapter_info.Text + ("类型:" + adapter.NetworkInterfaceType) + "\n";
		tb_net_wark_adapter_info.Text = tb_net_wark_adapter_info.Text + ("速度:" + adapter.Speed / 1000 / 1000 + "MB") + "\n";
		tb_net_wark_adapter_info.Text = tb_net_wark_adapter_info.Text + ("MAC 地址:" + adapter.GetPhysicalAddress()) + "\n";
		//获取IPInterfaceProperties实例
		IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
		//获取并显示DNS服务器IP地址信息
		IPAddressCollection dnsServers = adapterProperties.DnsAddresses;
		if (dnsServers.Count > 0) {
			foreach (IPAddress dns in dnsServers) {
				tb_net_wark_adapter_info.Text = tb_net_wark_adapter_info.Text + ("DNS 服务器IP地址:" + dns + "\n");
			}
		} else {
			tb_net_wark_adapter_info.Text = tb_net_wark_adapter_info.Text + ("DNS 服务器IP地址:" + "\n");
		}
    }
}

结果样例

---------------------第1个适配器信息---------------------

描述信息:以太网

类型:Ethernet

速度:0MB

MAC 地址:9883892C3E84

DNS 服务器IP地址:192.168.0.0

---------------------第2个适配器信息---------------------

描述信息:VirtualBox Host-Only Network #3

类型:Ethernet

速度:1000MB

MAC 地址:0A002700000B

DNS 服务器IP地址:fec0:0:0:ffff::1%1

DNS 服务器IP地址:fec0:0:0:ffff::2%1

DNS 服务器IP地址:fec0:0:0:ffff::3%1

...

十、声卡信息

1、声卡PNPDeviceID

public static string getSoundPNPID() {
    string st = "";
    ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_SoundDevice");
    foreach (ManagementObject mo in mos.Get()) {
		st = mo["PNPDeviceID"].ToString();
    }
    return st;
}

十一、其他信息

1、鼠标按钮个数

public static string getMouseButtons()  {
    return SystemInformation.MouseButtons.ToString();
}

十二、网络信息

1、网络连接

public static string getIsHasNet() {
    if (SystemInformation.Network) {
		return  "网络连接:已连接";
    } else {
		return "网络连接:未连接";
    }
}

2、IP地址

public static string getIP() {
    try {
		string st = "";
		ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
		ManagementObjectCollection moc = mc.GetInstances();
		foreach (ManagementObject mo in moc) {
			if ((bool)mo["IPEnabled"] == true) {
				System.Array ar;
				ar = (System.Array)(mo.Properties["IpAddress"].Value);
				st = ar.GetValue(0).ToString();
				break;
			}
		}
		moc = null;
		mc = null;
		return st;
    } catch {
		return "unknow";
    }
}
相关推荐
HoneyMoose2 分钟前
IDEA 2024.3 版本更新主要功能介绍
java·ide·intellij-idea
我只会发热3 分钟前
Java SE 与 Java EE:基础与进阶的探索之旅
java·开发语言·java-ee
是老余4 分钟前
本地可运行,jar包运行错误【解决实例】:通过IDEA的maven package打包多模块项目
java·maven·intellij-idea·jar
crazy_wsp5 分钟前
IDEA怎么定位java类所用maven依赖版本及引用位置
java·maven·intellij-idea
.Ayang7 分钟前
tomcat 后台部署 war 包 getshell
java·计算机网络·安全·web安全·网络安全·tomcat·网络攻击模型
一直学习永不止步13 分钟前
LeetCode题练习与总结:最长回文串--409
java·数据结构·算法·leetcode·字符串·贪心·哈希表
懷淰メ13 分钟前
PyQt飞机大战游戏(附下载地址)
开发语言·python·qt·游戏·pyqt·游戏开发·pyqt5
hummhumm27 分钟前
第 22 章 - Go语言 测试与基准测试
java·大数据·开发语言·前端·python·golang·log4j
chusheng184031 分钟前
Java项目-基于SpringBoot+vue的租房网站设计与实现
java·vue.js·spring boot·租房·租房网站
宁静@星空33 分钟前
006-自定义枚举注解
java·开发语言