Use C # To obtain computer hardware information

Source: Internet
Author: User

Use C # To obtain computer hardware information

Now I will explain in the future How to Use WMI (Windows Management Instrumentation) in C # To obtain computer hardware information.

The purpose is to use WMI api to obtain the following information about the computer in C:

Number of physical Processors
Number of logical processors
Number of digits
System Architecture
Number of kernels

Create a console application in Visual Studio, right-click the reference, select "add reference", and then select "System. Management ".

Now the using statement includes System. Management. You can use WMI class reference in your code.

Here is the complete code for generating hardware information.

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication8{                            class Program    {        static void Main(string[] args)        {            GetCpuDetails();        }        private static void GetCpuDetails()        {            foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get())            {                Console.WriteLine("Number Of Physical Processors: {0} ", item["NumberOfProcessors"]);                Console.WriteLine("Number Of Logical Processors: {0} ", item["NumberOfLogicalProcessors"]);            }            var numberOfCores = 0;            foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_Processor").Get())            {                numberOfCores += int.Parse(item["NumberOfCores"].ToString());                Console.WriteLine("Bitness: {0}", item["AddressWidth"]);                Console.WriteLine("Architecture: {0}", GetArchitectureDetail(int.Parse(item["Architecture"].ToString())));            }            Console.WriteLine("Number Of Cores: {0}", numberOfCores);        }        private static string GetArchitectureDetail(int architectureNumber)        {            switch (architectureNumber)            {                case 0: return "x86";                case 1: return "MIPS";                case 2: return "Alpha";                case 3: return "PowerPC";                case 6: return "Itanium-based systems";                case 9: return "x64";                default:                    return "Unkown";            }        }    }}

Here is the output of the above program.

You can also browse the Win32_Processor WMI class through Win32_ComputerSystem for more details.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.