var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); var cpuUsage = cpuCounter.NextValue(); Console.WriteLine($"CPU usage: {cpuUsage}%");
var memCounter = new PerformanceCounter("Memory", "Available Bytes"); var availableMem = memCounter.NextValue(); Console.WriteLine($"Available memory: {availableMem/1024/1024} MB");In this example, we create a new PerformanceCounter instance to get the available memory. We pass the parameters "Memory" and "Available Bytes". This will get the current amount of available memory in bytes. We then convert this to megabytes for easier readability. The PerformanceCounter class is part of the System.Diagnostics namespace in the .NET Framework.