C# 获取计算机cpu,硬盘,内存相关的信息
using System;
using System.Management;
namespace MmPS.Common.Helper
{
/// <summary>
/// 获取计算机相关的信息
/// </summary>
public class Computer
{
public static string CpuID; //1.cpu序列号
public static string MacAddress; //2.mac序列号
public static string DiskID; //3.硬盘id
public static string IpAddress; //4.ip地址
public static string LoginUserName; //5.登录用户名
public static string ComputerName; //6.计算机名
public static string SystemType; //7.系统类型
public static string TotalPhysicalMemory; //8.内存量 单位:M
static Computer()
{
CpuID = GetCpuID();
MacAddress = GetMacAddress();
DiskID = GetDiskID();
IpAddress = GetIPAddress();
LoginUserName = GetUserName();
SystemType = GetSystemType();
TotalPhysicalMemory = GetTotalPhysicalMemory();
ComputerName = GetComputerName();
}
public static Guid GetComputerCpuDiskMac()
{
string cpu = GetCpuID();
string disk = GetDiskID();
string mac = GetMacAddress();
string com = cpu + disk + mac;
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(com);
string result = MD5Help.GetMD5(buffer);
Guid guid = new Guid(result);
return guid;
}
/// <summary>
/// 获取CPU序列号代码
/// </summary>
/// <returns>CPU序列号代码</returns>
public static string GetCpuID()
{
try
{
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
{
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;
}
}
moc = null;
mc = null;
return mac;
}
catch
{
return "unknow";
}
finally
{
}
}
/// <summary>
/// 获取硬盘ID
/// </summary>
/// <returns>硬盘ID</returns>
public static string GetDiskID()
{
try
{
string HDid = "";
ManagementClass mc = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
HDid = (string)mo.Properties["Model"].Value;
}
moc = null;
mc = null;
return HDid;
}
catch
{
return "unknow";
}
finally
{
}
}
/// <summary>
/// 获取IP地址
/// </summary>
/// <returns></returns>
public static string GetIPAddress()
{
try
{
string st = "";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
//st=mo["IpAddress"].ToString();
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";
}
finally
{
}
}
/// <summary>
/// 操作系统的登录用户名
/// </summary>
/// <returns></returns>
public static string GetUserName()
{
try
{
string un = "";
un = Environment.UserName;
return un;
}
catch
{
return "unknow";
}
finally
{
}
}
/// <summary>
/// 获取计算机名
/// </summary>
/// <returns></returns>
public static string GetComputerName()
{
try
{
return System.Environment.MachineName;
}
catch
{
return "unknow";
}
finally
{
}
}
/// <summary>
/// 获取PC类型
/// </summary>
/// <returns>PC类型</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
{
string st = "";
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
st = mo["TotalPhysicalMemory"].ToString();
}
moc = null;
mc = null;
return st;
}
catch
{
return "unknow";
}
finally
{
}
}
}
}
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace MmPS.Common.Helper
{
public static class MD5Help
{
/// <summary>
/// 获取流的md5值
/// </summary>
/// <param name="stream">输入流</param>
/// <returns>输入流处理以后的md5值</returns>
public static string GetMD5(Stream stream)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] data = md5Hasher.ComputeHash(stream);
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
/// <summary>
/// 获取字节数组的md5值
/// </summary>
/// <param name="buffer">输入字节数组</param>
/// <returns>输入字节数组处理以后的md5值</returns>
public static string GetMD5(byte[] buffer)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] data = md5Hasher.ComputeHash(buffer);
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
return sBuilder.ToString();
}
}
}