码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2017-09-04 15:15:24      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:cores   img   system   mon   tcl   int   汇总   技术分享   roc   

获取计算机内部信息 - ComputerInfoHelper

一、代码

  ComputerInfoHelper.cs

    /// <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 Name = "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 ,具体容量可参考此公式计算。

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

标签:cores   img   system   mon   tcl   int   汇总   技术分享   roc   

原文地址:http://www.cnblogs.com/liqingwen/p/7473227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!