MAC地址
94:C6:91:71:9D:39
/// <summary>
/// MAC地址
/// </summary>
/// <returns></returns>
public static string GetPcMac()
{
ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration");
foreach (ManagementObject managementObject in mos.Get().OfType<ManagementObject>())
{
if (managementObject["MACAddress"] != null)
{
return managementObject["MACAddress"].ToString();
}
}
return string.Empty;
}
系统信息
Microsoft Windows 10 教育版;10.0.17763;
/// <summary>
/// 系统信息
/// </summary>
/// <returns></returns>
public static string GetPcSystemInfo()
{
StringBuilder sb = new StringBuilder(64);
ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_OperatingSystem");
foreach (ManagementObject managementObject in mos.Get().OfType<ManagementObject>())
{
if (managementObject["Caption"] != null)
{
sb.Append(managementObject["Caption"]);
sb.Append(";");
}
if (managementObject["Version"] != null)
{
sb.Append(managementObject["Version"]);
sb.Append(";");
}
}
return sb.ToString();
}
.net信息
4.8 or later
/// <summary>
/// .net信息
/// </summary>
/// <returns></returns>
public static string GetPcNetInfo()
{
// 查找较新的.NET Framework版本(4.5及更高版本)
const string subkey = @"SOFTWAREMicrosoftNET Framework SetupNDPv4Full";
using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
{
if (ndpKey != null && ndpKey.GetValue("Release") != null)
{
int releaseKey = (int)ndpKey.GetValue("Release");
if (releaseKey >= 528040)
return "4.8 or later";
if (releaseKey >= 461808)
return "4.7.2";
if (releaseKey >= 461308)
return "4.7.1";
if (releaseKey >= 460798)
return "4.7";
if (releaseKey >= 394802)
return "4.6.2";
if (releaseKey >= 394254)
return "4.6.1";
if (releaseKey >= 393295)
return "4.6";
if (releaseKey >= 379893)
return "4.5.2";
if (releaseKey >= 378675)
return "4.5.1";
if (releaseKey >= 378389)
return "4.5";
return "No 4.5 or later version detected";
}
else
{
// 查找较旧的 .NET Framework 版本 (1-4)
DirectoryInfo[] directories = new DirectoryInfo(Environment.SystemDirectory + @"..Microsoft.NETFramework").GetDirectories("v?.?.*");
return directories.Last()?.Name;
}
}
}
VC++信息
Microsoft Visual C++ 2019 X86 Minimum Runtime - 14.23.27820;Microsoft Visual C++ 2013 Redistributable (x64) - 12.0.30501;Microsoft Visual C++ 2013 x86 Minimum Runtime - 12.0.21005;Microsoft Visual C++ 2012 Redistributable (x86) - 11.0.61030;Microsoft Visual C++ 2015-2019 Redistributable (x86) - 14.23.27820;Microsoft Visual C++ 2015-2019 Redistributable (x64) - 14.23.27820;Microsoft Visual C++ 2019 X86 Additional Runtime - 14.23.27820;Microsoft Visual C++ 2008 Redistributable - x86 9.0.30729.17;Microsoft Visual C++ 2019 X86 Debug Runtime - 14.23.27820;Microsoft Visual C++ 2012 x86 Additional Runtime - 11.0.61030;Microsoft Visual C++ 2012 x86 Minimum Runtime - 11.0.61030;Microsoft Visual C++ 2013 Redistributable (x86) - 12.0.30501;Microsoft Visual C++ 2013 x86 Additional Runtime - 12.0.21005;
/// <summary>
/// VC++信息
/// </summary>
/// <returns></returns>
public static string GetPcVcInfo()
{
var sb = new StringBuilder(128);
string subkey = string.Empty;
if (Environment.Is64BitProcess)
{
subkey = @"SOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionUninstall";
}
else
{
subkey = @"SOFTWAREMicrosoftWindowsCurrentVersionUninstall";
}
using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
{
var names = ndpKey.GetSubKeyNames();
foreach (var name in names)
{
using (var key = ndpKey.OpenSubKey(name))
{
var val = key.GetValue("DisplayName");
if (val != null && val.ToString().Contains("Microsoft Visual C++"))
{
Console.WriteLine(val);
sb.Append(val);
sb.Append(";");
}
}
}
}
return sb.ToString();
}
占用内存(MB)
/// <summary>
/// 占用内存(MB)
/// </summary>
/// <returns></returns>
public static string GetProcMemory()
{
var proc = System.Diagnostics.Process.GetCurrentProcess();
var pf1 = new System.Diagnostics.PerformanceCounter("Process", "Working Set - Private", proc.ProcessName);//第二个参数就是得到只有工作集
var usedMemory = pf1.NextValue() / 1024 / 1024;
return $"{usedMemory:F1} MB";
}
CPU使用率(%)
/// <summary>
/// CPU使用率(%)
/// </summary>
/// <returns></returns>
public static string GetProcCPU()
{
var proc = System.Diagnostics.Process.GetCurrentProcess();
double percent = 0.0;
var query = new ObjectQuery("select * from Win32_Process where ProcessID=" + proc.Id);
var mos = new ManagementObjectSearcher(query);
foreach (ManagementObject item in mos.Get().OfType<ManagementObject>())
{
try
{
if (_firstSample == DateTime.MinValue)
{
_firstSample = DateTime.Now;
item.Get();
_oldCPU = Convert.ToInt64(item["UserModeTime"]) + Convert.ToInt64(item["KernelModeTime"]);
}
else
{
var secondSample = DateTime.Now;
item.Get();
var newCpu = Convert.ToInt64(item["UserModeTime"]) + Convert.ToInt64(item["KernelModeTime"]);
var msPassed = (secondSample - _firstSample).TotalMilliseconds;
percent = (newCpu - _oldCPU) / (msPassed * 100.0 * Environment.ProcessorCount);
_oldCPU = newCpu;
_firstSample = secondSample;
}
}
catch (Exception ex)
{
LocalParking.LogFactory.Error(null, ex);
}
}
return $"{percent:F1}%";
}
static DateTime _firstSample = DateTime.MinValue;
static long _oldCPU = 0;