码迷,mamicode.com
首页 > 其他好文 > 详细

VMI帮助类

时间:2020-12-23 12:21:27      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:console   bit   ORC   win32   directory   windows   adapter   contain   visual   

MAC地址

/// <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;
}

系统信息

/// <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版本信息

/// <summary>
/// .net信息
/// </summary>
/// <returns></returns>
public static string GetPcNetInfo()
{
    // 查找较新的.NET Framework版本(4.5及更高版本)
    const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
    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.NET\Framework").GetDirectories("v?.?.*");
            return directories.Last()?.Name;
        }
    }
}

VC++信息

/// <summary>
/// VC++信息
/// </summary>
/// <returns></returns>
public static string GetPcVcInfo()
{
    var sb = new StringBuilder(128);
    string subkey = string.Empty;
    if (Environment.Is64BitProcess)
    {
        subkey = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\";
    }
    else
    {
        subkey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\";
    }
    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使用率(%)

private static DateTime _firstSample = DateTime.MinValue;
private static long _oldCPU = 0;
/// <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($"CPU使用率异常", ex);
        }
    }
    return $"{percent:F1}%";
}


VMI帮助类

标签:console   bit   ORC   win32   directory   windows   adapter   contain   visual   

原文地址:https://www.cnblogs.com/wesson2019-blog/p/14156286.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!