• [C#] 获取计算机内部信息


    获取计算机内部信息 - ComputerInfoHelper

      电脑有许多信息,如名称、CPU 和硬盘容量等,整理一个 Helper 来获取,下面目前只整理了一个方法,获取其它信息的方法类似。

    一、代码

      ComputerInfoHelper.cs

    using System;
    using System.Linq;
    using System.Management;
    using Wen.Helpers.Common.Computer.Config;
    
    namespace Wen.Helpers.Common.Computer
    {
        /// <summary>
        /// 计算机信息助手类
        /// </summary>
        public class ComputerInfoHelper
        {
            /// <summary>
            /// 获取硬盘容量汇总
            /// </summary>
            public static long GetTotalDiskSize()
            {
                try
                {
                    var diskDriveManagement = new ManagementClass(WmiPath.DiskDrive);
                    var diskDriveManagementInstances = diskDriveManagement.GetInstances();
    
                    return diskDriveManagementInstances.OfType<ManagementObject>()
                        .Select(managementObject => Convert.ToInt64(managementObject[ManagementBaseObjectPropertyName.Size]))
                        .Aggregate<long, long>(0, (current, diskSize) => diskSize + current);
                }
                catch(Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
        }
    }

      ManagementBaseObjectPropertyName.cs

    namespace Wen.Helpers.Common.Computer.Config
    {
        public class ManagementBaseObjectPropertyName
        {
            /// <summary>
            /// 名称
            /// </summary>
            public const string ComputerName = "Name";
    
            /// <summary>
            /// 显卡芯片
            /// </summary>
            public const string VideoProcessor = "VideoProcessor";
    
            /// <summary>
            /// 显存大小
            /// </summary>
            public const string AdapterRam = "AdapterRAM";
    
            /// <summary>
            /// 分辨率宽
            /// </summary>
            public const string ScreenWidth = "ScreenWidth";
    
            /// <summary>
            /// 分辨率高
            /// </summary>
            public const string ScreenHeight = "ScreenHeight";
    
            /// <summary>
            /// 电脑型号
            /// </summary>
            public const string Version = "Version";
    
            /// <summary>
            /// 硬盘容量
            /// </summary>
            public const string Size = "Size";
    
            /// <summary>
            /// 内存容量
            /// </summary>
            public const string Capacity = "Capacity";
    
            /// <summary>
            /// cpu 核心数
            /// </summary>
            public const string NumberOfCores = "NumberOfCores";
        }
    }

      WmiPath.cs

    namespace Wen.Helpers.Common.Computer.Config
    {
        public class WmiPath
        {
            /// <summary>
            /// 内存
            /// </summary>
            public const string PhysicalMemory = "Win32_PhysicalMemory";
    
            /// <summary>
            /// cpu
            /// </summary>
            public const string Processor = "Win32_Processor";
    
            /// <summary>
            /// 硬盘
            /// </summary>
            public const string DiskDrive = "win32_DiskDrive";
    
            /// <summary>
            /// 电脑型号
            /// </summary>
            public const string ComputerSystemProduct = "Win32_ComputerSystemProduct";
    
            /// <summary>
            /// 分辨率
            /// </summary>
            public const string DesktopMonitor = "Win32_DesktopMonitor";
    
            /// <summary>
            /// 显卡
            /// </summary>
            public const string VideoController = "Win32_VideoController";
    
            /// <summary>
            /// 操作系统
            /// </summary>
            public const string OperatingSystem = "Win32_OperatingSystem";
        }
    }

    二、测试

      1.电脑硬盘信息

       2.我的电脑中的硬盘信息

       3.测试代码

       4.输出结果:

       所有硬盘及 U 盘和存储卡,因生产厂家的计算方式和操作系统的计算方式不一样,前者按 1MB = 1000KB 计算,后者按 1MB = 1024KB 计算,所以,实际在电脑中看到的容量会与厂商标称的容量有一定的差异,其中规律为 1G = 0.93G (约等于),比如 100G,实际为 100G * 0.93 = 93G ,具体容量可参考此公式计算。

      GitHub:https://github.com/liqingwen2015/Wen.Helpers/tree/liqw_home/Wen.Helpers.Common/Computer

  • 相关阅读:
    开源魔兽世界私服搭建
    centos7 普通用户无法使用ssh登录其他服务器
    Java时间格式大全
    C#中的线程之Abort陷阱
    C# 多线程学习系列四之ThreadPool取消、超时子线程操作以及ManualResetEvent和AutoResetEvent信号量的使用
    C# ThreadPool类(线程池)
    VS2019输出信息到调试控制台
    Stream/Bytes[]/Image对象相互转化
    Asp.NetCore 读取配置文件帮助类
    Java SpringBoot使用126邮箱发送html内容邮件,带附件
  • 原文地址:https://www.cnblogs.com/liqingwen/p/7473227.html
Copyright © 2020-2023  润新知