get system cpu frequency in csharp

You can get the system CPU frequency by using the ManagementObjectSearcher class from the System.Management namespace. Here's an example code:

main.cs
using System.Management;

namespace SystemInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagementObjectSearcher myProcessorObject = new ManagementObjectSearcher("select * from Win32_Processor");
            foreach (ManagementObject obj in myProcessorObject.Get())
            {
                var cpuFrequency = (uint)obj["MaxClockSpeed"];
                Console.WriteLine("CPU Frequency: {0}MHz", cpuFrequency);
            }
        }
    }
}
496 chars
18 lines

This program creates a ManagementObjectSearcher object to search for CPU-related information on the system. The query parameter passed to the constructor of ManagementObjectSearcher specifies to select all the properties of the Win32_Processor class.

Then, the program loops through the resulting collection of ManagementObject objects and gets the value of the MaxClockSpeed property, which represents the maximum clock speed of the CPU. This value is cast to an unsigned integer and printed to the console.

Note that this code snippet only retrieves the maximum clock speed of the CPU. If you want to get additional information about the CPU, such as the number of cores or the manufacturer, you can modify the query parameter passed to ManagementObjectSearcher according to the properties of the Win32_Processor class.

gistlibby LogSnag