• VMI帮助类


    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 = @"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++信息

    /// <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使用率(%)

    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}%";
    }
    

    
    
  • 相关阅读:
    js1
    curl获取图片
    TP中讲的两种ajax方法
    tp验证码
    TP上传图片
    TP中登录验证
    tp中自定义跳转页面
    用户数据库表状态一类的问题
    用php实现斐波那契数列
    微信的网页授权登陆
  • 原文地址:https://www.cnblogs.com/wesson2019-blog/p/14156286.html
Copyright © 2020-2023  润新知