隐藏

winform c# 获取电脑配置信息和cpu 内存使用率

发布:2022/11/10 11:25:43作者:管理员 来源:本站 浏览次数:769

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Windows.Forms;
using MaterialSkin.Controls;
using MaterialSkin;
using SerialCommunicate;
using Decimal = SerialCommunicate.Decimal;
using System.Management;
 
namespace WindowsFormsApp2
{
 
    public partial class Form1 : MaterialForm
    {
        
        PerformanceCounter cpuCounter;
        PerformanceCounter ramCounter;
        
        
 
        private readonly MaterialSkinManager materialSkinManager;
        public Form1()
        {
            InitializeComponent();
            //主题构造函数
            materialSkinManager = MaterialSkinManager.Instance;
            materialSkinManager.EnforceBackcolorOnAllComponents = true;
            materialSkinManager.AddFormToManage(this);
            //主题参数
            
            materialSkinManager.Theme = MaterialSkinManager.Themes.DARK;
            materialSkinManager.ColorScheme = new ColorScheme( Primary.Grey700,Primary.Grey900,Primary.Grey600,Accent.Amber400,TextShade.WHITE);
            cpuCounter = new PerformanceCounter();
            cpuCounter.CategoryName = "Processor";
            cpuCounter.CounterName = "% Processor Time";
            cpuCounter.InstanceName = "_Total";
            cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            ramCounter = new PerformanceCounter("Memory", "Available MBytes");
            
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.AppendText("\r\n" + "-----------------------------------------------------");
            string a = null; string a2 = null; string a4 = null; string a5 = null; string a1 = null; string a3 = null;
            ManagementObjectSearcher searcherd1 = new ManagementObjectSearcher("Select * From Win32_Processor");
            foreach (ManagementObject mo in searcherd1.Get())
            {
                a += mo["Name"].ToString().Trim() + "; ";
                a1 += mo["NumberOfCores"].ToString().Trim();
                a2 += mo["NumberOfLogicalProcessors"].ToString().Trim();
                a3 += mo["CurrentClockSpeed"].ToString().Trim();
                a4 += mo["DataWidth"].ToString().Trim() + "; ";
                a5 += mo["ProcessorId"].ToString().Trim() + "; ";
            }
            //处理器信息
            searcherd1.Dispose();
            textBox1.AppendText("\r\n"+ "处理器:     " + a);
            textBox1.AppendText("\r\n" + "核心:        " + a1 + "核心; ");
            textBox1.AppendText("\r\n" + "线程:        " + a2 + "线程; ");
            textBox1.AppendText("\r\n" + "主频:        " + a3.Substring(0, 1) + "." + a3.Substring(1, 2) + "Ghz; ");
            textBox1.AppendText("\r\n" + "架构:           " + a4);
            textBox1.AppendText("\r\n" + "CPUID:     " + a5);
            textBox1.AppendText("\r\n" + "-----------------------------------------------------" );
            //主板
            string b = null; string b2 = null; string b4 = null;
            string b1 = null; string b3 = null; string b5 = null;
          
            ManagementObjectSearcher searcherd2 = new ManagementObjectSearcher("Select * From Win32_BaseBoard");
            foreach (ManagementObject mo in searcherd2.Get())
            {
                 b += mo["Product"].ToString().Trim() + "; ";
                 b1 += mo["Manufacturer"].ToString().Trim() + "; ";
                 b2 += mo["SerialNumber"].ToString().Trim() + "; ";
                 b3 += mo["CreationClassName"].ToString().Trim() + "; ";
                 b4 += mo["HotSwappable"].ToString().Trim() + "; ";
                 b5 += mo["Status"].ToString().Trim() + "; ";
             }
            searcherd2.Dispose();
            string systemboard = b1.ToUpper();
            if (systemboard.IndexOf("MICRO-STAR") != -1)
                textBox1.AppendText("微星 " + b);
            else if (systemboard.IndexOf("GALAX") != -1)
                textBox1.AppendText("\r\n" + "影驰 " + b);
            else if (systemboard.IndexOf("LENOVO") != -1)
                textBox1.AppendText("\r\n" + "联想 " + b);
            else if (systemboard.IndexOf("ASUS") != -1 || systemboard.IndexOf("ROG") != -1)
                textBox1.AppendText("\r\n" + "华硕 " + b);
            else if (systemboard.IndexOf("ASROCK") != -1)
                textBox1.AppendText("\r\n" + "华擎 " + b);
            else if (systemboard.IndexOf("GIGABYTE") != -1)
                textBox1.AppendText("\r\n" + "技嘉 " + b);
            else if (systemboard.IndexOf("SAPPHIRE") != -1)
                textBox1.AppendText("\r\n" + "蓝宝石 " + b);
            else if (systemboard.IndexOf("ACER") != -1)
                textBox1.AppendText("\r\n" + "宏碁 " + b);
            else if (systemboard.IndexOf("COLORFUL") != -1)
                textBox1.AppendText("\r\n" + "七彩虹 " + b);
            else if (systemboard.IndexOf("DELL") != -1)
                textBox1.AppendText("\r\n" + "戴尔 " + b);
            else if (systemboard.IndexOf("ONDA") != -1)
                 textBox1.AppendText("\r\n" + "昂达 " + b);
            else if (systemboard.IndexOf("BIOSTAR") != -1)
                textBox1.AppendText("\r\n" + "映泰 " + b);
            else if (systemboard.IndexOf("MAXSUN") != -1)
                textBox1.AppendText("\r\n" + "铭瑄 " + b);
            else if (systemboard.IndexOf("SOYO") != -1)
                textBox1.AppendText("\r\n" + "梅捷 " + b);
            else if (systemboard.IndexOf("TOPSTAR") != -1)
                textBox1.AppendText("\r\n" + "顶星 " + b);
            else if (systemboard.IndexOf("CLEVO") != -1)
                textBox1.AppendText("\r\n" + "蓝天 " + b);
            else if (systemboard.IndexOf("Quanta") != -1)
                textBox1.AppendText("\r\n" + "广达 " + b);
            else if (systemboard.IndexOf("HASEE") != -1)
                textBox1.AppendText("\r\n" + "磐英 " + b);
            else if (systemboard.IndexOf("HUANAN") != -1)
                textBox1.AppendText("\r\n" + "华南 " + b);
            else if (systemboard.IndexOf("HP") != -1)
                textBox1.AppendText("\r\n" +"主板:"+ "惠普 " + b);
            else textBox1.AppendText(b);
 
            textBox1.AppendText("\r\n" + "主板厂家:     " + b1);
            textBox1.AppendText("\r\n" + "主板型号:     " + b);
            textBox1.AppendText("\r\n" + "主板序列:     " + b2);
            textBox1.AppendText("\r\n" + "主板类名:     " + b3);
            textBox1.AppendText("\r\n" + "热插拔:        " + b4);
            textBox1.AppendText("\r\n" + "主板状态:     " + b5);
            textBox1.AppendText("\r\n" + "-----------------------------------------------------");
            //内存
            double capacity = 0; string D = null; string D2 = null; string D4 = null; string D5 = null; string D1 = null; string D3 = null; string D6 = null;
            
            
                ManagementObjectSearcher searcherd5 = new ManagementObjectSearcher("Select * From Win32_PhysicalMemory");
                foreach (ManagementObject mo in searcherd5.Get())
                {
                    try
                    {
                        capacity += Math.Round(Int64.Parse(mo.Properties["Capacity"].Value.ToString().Trim()) / 1024 / 1024 / 1024.0, 1);
                    }
                    catch (Exception)
                    {
 
                        capacity = 0;
                    }
                    
                    D += Math.Round(Int64.Parse(mo.Properties["Capacity"].Value.ToString()) / 1024 / 1024 / 1024.0, 1).ToString() + "G" + " (" + mo["Speed"].ToString() + "MHz) " + "; ";
                    D1 += mo["manufacturer"].ToString().Trim() + "; ";
                    D2 += mo["Speed"].ToString().Trim() + "MHz; ";
                    D3 += Math.Round(Int64.Parse(mo.Properties["Capacity"].Value.ToString().Trim()) / 1024 / 1024 / 1024.0, 1).ToString() + "G; ";
                    D4 += mo["SerialNumber"].ToString().Trim() + "; ";
                    D5 += mo["DataWidth"].ToString().Trim() + "; ";
                    D6 += mo["DeviceLocator"].ToString().Trim() + "; ";
 
                    searcherd5.Dispose();
                }
            if (systemboard.IndexOf("GALAXY MICROSYSTEMS LTD") != -1)
            {
 
                textBox1.AppendText("\r\n" + "内存厂商:     " + systemboard.Replace("GALAXY MICROSYSTEMS LTD", "影驰"));
            }
            else if (systemboard.IndexOf("SAMSUNG") != -1)
            {
                textBox1.AppendText("\r\n" + "内存厂商:     " + systemboard.Replace("SAMSUNG", "三星"));
 
            }
            else if (systemboard.IndexOf("04CB") != -1)
            {
                textBox1.AppendText("\r\n" + "内存厂商:     " + systemboard.Replace("04CB", "威刚"));
 
            }
            else
                textBox1.AppendText("\r\n" + "内存厂商:     " + D1);
 
            if (capacity != 0)
            {
                textBox1.AppendText("\r\n" + D + "共" + capacity + "G; ");
            }
            else textBox1.AppendText(D);
            textBox1.AppendText("\r\n" + "内存频率:     " + D2);
            textBox1.AppendText("\r\n" + "内存容量:     " + D3);
            textBox1.AppendText("\r\n" + "内存总量:     " + capacity + "G; ");
            textBox1.AppendText("\r\n" + "序列号:        " + D4);
            textBox1.AppendText("\r\n" + "数据宽度:     " + D5);
            textBox1.AppendText("\r\n" + "控制器:        " + D6);
 
 
 
 
 
 
        }
       
      
 
        private void materialButton2_Click(object sender, EventArgs e)
        {
            serial frm2 = new serial();
            frm2.Show();
        }
 
        private void materialButton3_Click(object sender, EventArgs e)
        {
            Decimal frm2 = new Decimal();
            frm2.Show();
        }
 
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //textBox1.Text = "组件初始化";
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            //cpu利用率
            float cpu = cpuCounter.NextValue();
            String num = cpu.ToString("0.0");
           label1.Text="CPU使用率:" + num + " %";
            //内存利用率
           label2.Text = ("可使用内存:" + (ramCounter.NextValue()/160).ToString("0.0") + " %");
        }
 
        private string ToString(float v)
        {
            throw new NotImplementedException();
        }
 
       
    }
}