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

获取计算机内部信息 - 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

 

posted @ 2017-09-04 13:37  反骨仔  阅读(1170)  评论(6编辑  收藏  举报