C# 获取电脑信息
作者:互联网
有些时候我们需要获取电脑的一些硬件信息,然后去配置不同的逻辑,也或者有根据部分硬件信息做机器验证的,当然你也可以做成一个小工具以方便查看信息;
本篇写一下获取部分电脑硬件信息的操作;
实现功能:
获取电脑硬件信息
开发环境:
开发工具: Visual Studio 2013
.NET Framework版本:4.5
实现代码:
//需要添加引用:System.Management;
//需要导入命名空间:using System.Management;
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
//CPU序列号
Console.WriteLine(mo.Properties["ProcessorId"].Value.ToString());
//若想获取所有属性,可迭代ManagementObject.Properties,以下同理;
//foreach (PropertyData pd in mo.Properties)
//{
// Console.WriteLine(pd.Name + "----" + pd.Value);
//}
}
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
//MAC地址
Console.WriteLine(mo["MacAddress"].ToString());
//ip地址
Console.WriteLine(((Array)mo.Properties["IpAddress"].Value).GetValue(0).ToString());
}
}
mc = new ManagementClass("Win32_DiskDrive");
moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
//硬盘ID
Console.WriteLine(mo.Properties["Model"].Value.ToString());
}
mc = new ManagementClass("Win32_ComputerSystem");
moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
//系统名称
Console.WriteLine(mo["Name"].ToString());
//登录用户名
Console.WriteLine(mo["UserName"].ToString());
//pc类型
Console.WriteLine(mo["SystemType"].ToString());
//内存
Console.WriteLine(mo["TotalPhysicalMemory"].ToString());
//电脑制造商
Console.WriteLine(mo["Manufacturer"].ToString());
//电脑型号
Console.WriteLine(mo["SystemFamily"].ToString());
}
//获取系统名称2
Console.WriteLine(System.Environment.GetEnvironmentVariable("ComputerName"));
由简入繁,拿来即用
后续精彩,持续关注
标签:Console,mc,C#,mo,电脑,获取,ToString,WriteLine,moc 来源: https://blog.csdn.net/qq_27410185/article/details/120815136