zoukankan      html  css  js  c++  java
  • c# 获取操作系统的硬件信息

    c# 获取系统本身硬件的信息

    需要添加 System.Management 程序集

    1. 获取操作系统名称
    ManagementClass manag = new ManagementClass("Win32_OperatingSystem");
    ManagementObjectCollection managCollection = manag.GetInstances();
    foreach (ManagementObject m in managCollection)
    {
        Console.WriteLine(m["Name"].ToString();
        
    break;
    }

      2. 获取CPU信息

    ManagementClass mcCPU = new ManagementClass("Win32_Processor");
    ManagementObjectCollection mocCPU = mcCPU.GetInstances();
    foreach (ManagementObject m in mocCPU)
    {
           Console.WriteLine(m["Name"].ToString());
           break;
    }

      3. 获取CPU核心数量

    int coreCount = 0;
    foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_Processor ").Get())
    {
           coreCount += int.Parse(item["NumberOfCores"].ToString());
    }
    
    Console.WriteLine(coreCount.ToString() + "");

      4. 获取系统内存大小

    ManagementObjectSearcher searcher = new ManagementObjectSearcher();   //用于查询一些如系统信息的管理对象
    searcher.Query = new SelectQuery("Win32_PhysicalMemory", "", new string[] { "Capacity" });//设置查询条件
    ManagementObjectCollection collection = searcher.Get();   //获取内存容量 
    ManagementObjectCollection.ManagementObjectEnumerator em = collection.GetEnumerator();
    long capacity = 0;
    while (em.MoveNext())
    {
        ManagementBaseObject baseObj = em.Current;
        if (baseObj.Properties["Capacity"].Value != null)
        {
            capacity += long.Parse(baseObj.Properties["Capacity"].Value.ToString());
        }
    }
    Console.WriteLine(capacity / 1024 / 1024 / 1024 + "GB");

      4. 获取硬盘空间

    System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
    long totalFreeSpace = 0;
    long totalDiskSize = 0;
    foreach (var drive in drives)
    {
        if (drive.IsReady)  //判断代码运行时 磁盘是可操作作态 
        {
            totalFreeSpace += drive.AvailableFreeSpace;
            totalDiskSize += drive.TotalSize;
        }
    }
    Console.WriteLine(string.Format("总容量:{0} 剩余:{1}", totalDiskSize / 1024 / 1024 / 1024 + "GB", totalFreeSpace / 1024 / 1024 / 1024 + "GB"));

      5. 获取屏幕分辨率

    ManagementClass manage = new ManagementClass("Win32_DesktopMonitor");
    ManagementObjectCollection manageCollection = manage.GetInstances();
    foreach (ManagementObject m in manageCollection)
    {
        Console.WriteLine(m["ScreenWidth"].ToString() + "*" + m["ScreenHeight"].ToString());
        break;
    }

      6. 获取显卡芯片名称 与 显存大小

    ManagementClass manage = new ManagementClass("Win32_VideoController");
    ManagementObjectCollection manageCollection = manage.GetInstances();
    foreach (ManagementObject m in manageCollection)
    {
        Console.WriteLine(m["VideoProcessor"].ToString().Replace("Family", ""));
        Console.WriteLine((Convert.ToInt64(m["AdapterRAM"]) / 1024 / 1024 / 1024) + "GB" );
        break;
    }
  • 相关阅读:
    如何提高代码质量(管理篇):代码复查
    Codeforce 239 B. Easy Tape Programming
    Codeforces Round #530 (Div. 1) 1098A Sum in the tree
    Codeforce 1098-A
    8月5日加油吧,菜鸡
    8月5日训练日记
    疯子的算法总结(七) 字符串算法之 manacher 算法 O(N)解决回文串
    疯子的算法总结(六) 复杂排序算法 ① 归并排序 merge_sort()
    疯子的算法总结(五) 矩阵乘法 (矩阵快速幂)
    疯子的算法总结(四)贪心算法
  • 原文地址:https://www.cnblogs.com/applebox/p/11611250.html
Copyright © 2011-2022 走看看