当前位置: 首页 > article >正文

C#获取计算机信息

目录

效果

项目

代码

下载


效果

项目

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;

namespace 获取计算机信息
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取计算机硬件信息
        /// </summary>
        /// <param name="path">部件</param>
        /// <param name="key">键值</param>
        /// <returns></returns>
        public string GetComputerHardWareInfo(string path, string key)
        {
            try
            {
                ManagementClass managementClass = new ManagementClass(path);
                ManagementObjectCollection moc = managementClass.GetInstances();
                PropertyDataCollection properties = managementClass.Properties;
                foreach (var property in properties)
                {
                    if (property.Name == key)
                    {
                        foreach (var mo in moc)
                        {
                            return mo.Properties[property.Name].Value.ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //记录异常信息
            }

            return string.Empty;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //获取CPU ID
            string CPU_ID = GetComputerHardWareInfo("Win32_Processor", "ProcessorId");
            //获取主板序列号
            string Board_SN = GetComputerHardWareInfo("Win32_BaseBoard", "SerialNumber");
            //获取硬盘序列号
            string Disk_SN = GetComputerHardWareInfo("Win32_DiskDrive", "Model");
            string UUID = GetComputerHardWareInfo("Win32_ComputerSystemProduct", "UUID");
            // 获取机器名
            string MachineName = Environment.MachineName;
            string OSVersion = Environment.OSVersion.VersionString.ToString();
            string UserName = Environment.UserName;
            string OperatingSystem = GetComputerHardWareInfo("Win32_OperatingSystem", "Caption");

            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Format("CPU_ID:{0}", CPU_ID));
            sb.AppendLine(string.Format("Board_SN:{0}", Board_SN));
            sb.AppendLine(string.Format("Disk_SN:{0}", Disk_SN));
            sb.AppendLine(string.Format("UUID:{0}", UUID));
            sb.AppendLine(string.Format("MachineName:{0}", MachineName));
            sb.AppendLine(string.Format("OSVersion:{0}", OSVersion));
            sb.AppendLine(string.Format("UserName:{0}", UserName));
            sb.AppendLine(string.Format("OperatingSystem:{0}", OperatingSystem));

            textBox1.Text = sb.ToString();
        }


    }
}
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;

namespace 获取计算机信息
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取计算机硬件信息
        /// </summary>
        /// <param name="path">部件</param>
        /// <param name="key">键值</param>
        /// <returns></returns>
        public string GetComputerHardWareInfo(string path, string key)
        {
            try
            {
                ManagementClass managementClass = new ManagementClass(path);
                ManagementObjectCollection moc = managementClass.GetInstances();
                PropertyDataCollection properties = managementClass.Properties;
                foreach (var property in properties)
                {
                    if (property.Name == key)
                    {
                        foreach (var mo in moc)
                        {
                            return mo.Properties[property.Name].Value.ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //记录异常信息
            }

            return string.Empty;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //获取CPU ID
            string CPU_ID = GetComputerHardWareInfo("Win32_Processor", "ProcessorId");
            //获取主板序列号
            string Board_SN = GetComputerHardWareInfo("Win32_BaseBoard", "SerialNumber");
            //获取硬盘序列号
            string Disk_SN = GetComputerHardWareInfo("Win32_DiskDrive", "Model");
            string UUID = GetComputerHardWareInfo("Win32_ComputerSystemProduct", "UUID");
            // 获取机器名
            string MachineName = Environment.MachineName;
            string OSVersion = Environment.OSVersion.VersionString.ToString();
            string UserName = Environment.UserName;
            string OperatingSystem = GetComputerHardWareInfo("Win32_OperatingSystem", "Caption");

            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Format("CPU_ID:{0}", CPU_ID));
            sb.AppendLine(string.Format("Board_SN:{0}", Board_SN));
            sb.AppendLine(string.Format("Disk_SN:{0}", Disk_SN));
            sb.AppendLine(string.Format("UUID:{0}", UUID));
            sb.AppendLine(string.Format("MachineName:{0}", MachineName));
            sb.AppendLine(string.Format("OSVersion:{0}", OSVersion));
            sb.AppendLine(string.Format("UserName:{0}", UserName));
            sb.AppendLine(string.Format("OperatingSystem:{0}", OperatingSystem));

            textBox1.Text = sb.ToString();
        }


    }
}

下载

源码下载


http://www.kler.cn/a/306179.html

相关文章:

  • 越南很火的slots游戏投放Google谷歌广告策略
  • 【Kafka】集成案例:与Spark大数据组件的协同应用
  • 网络延迟对Python爬虫速度的影响分析
  • Spring——事务
  • 力扣-Mysql-3308- 寻找表现最佳的司机(中等)
  • web安全测试渗透案例知识点总结(上)——小白入狱
  • C++入门基础知识68(高级)——【关于C++ 异常处理】
  • 【系统架构设计师-2010年真题】案例分析-答案及详解
  • Superset二次开发之源码asyncEvent.ts 分析
  • 嵌入式C语言自我修养:C语言的面向对象编程思想
  • 问题 H: 三角数
  • 【在Linux世界中追寻伟大的One Piece】五种IO模型和阻塞IO
  • 13. 神经网络基本骨架--nn.Module
  • 长业务事务的离线并发问题
  • 9. 什么是 Beam Search?深入理解模型生成策略
  • leetcode 难度【简单模式】标签【数据库】题型整理大全
  • 【网络安全的神秘世界】渗透测试基础
  • 【C#】添加临时环境变量
  • linux第二课(docker的安装使用)
  • 微软九月补丁星期二发现了 79 个漏洞
  • 《ImageNet Classification with Deep Convolutional Neural Networks》论文导读
  • 漫画元素检测系统源码分享
  • AutoSar AP通信的事件订阅
  • Playwright与Selenium的对比:谁是更适合你的自动化测试工具?
  • 通俗理解低秩分解
  • 【webpack4系列】设计可维护的webpack4.x+vue构建配置(终极篇)