[C#] 获取计算机内部信息_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > [C#] 获取计算机内部信息

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

 2017/9/4 15:08:57  ComputerInfoHelper  程序员俱乐部  我要评论(0)
  • 摘要:获取计算机内部信息-ComputerInfoHelper一、代码ComputerInfoHelper.cs///<summary>///计算机信息助手类///</summary>publicclassComputerInfoHelper{///<summary>///获取硬盘容量汇总///</summary>publicstaticlongGetTotalDiskSize(){try
  • 标签:计算机 C#

获取计算机内部信息 - 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# 匿名对象(匿名类型)、var、动态类型 dynamic 下一篇: 没有下一篇了!
发表评论
用户名: 匿名