Is there a way to show CPU and RAM usage statistics on an asp.net page. I've tried this code but I have error:
Access to the registry key 'Global' is denied.
on this line:
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
Is there a way to show CPU and RAM usage statistics on an asp.net page. I've tried this code but I have error:
Access to the registry key 'Global' is denied.
on this line:
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
As mentioned in comments, without the appropriate permissions you will not be able to do this. Resource statistics will include a lot of information about processes owned by other users of the system which is privileged information
 
    
    Use:
    System.Diagnostics.PerformanceCounter cpuUsage = 
      new System.Diagnostics.PerformanceCounter();
    cpuUsage.CategoryName = "Processor"; 
    cpuUsage.CounterName = "% Processor Time";
    cpuUsage.InstanceName = "_Total";
    float f = cpuUsage.NextValue();
Edit:
Windows limits access to the performance counters to those in the Administrators or Performance Logs Users (in Vista+) groups. Setting registry security won't resolve this. It is possible that there is a user right attached to it somewhere.
 
    
    to do not call .NextValue() twice you can use MVC "global variables:
    [AllowAnonymous]
    [HttpGet]
    [ActionName("serverusage")]
    public HttpResponseMessage serverusage()
    {
        try
        {
            PerformanceCounter cpuCounter;
            if (HttpContext.Current.Application["cpuobj"] == null)
            {
                cpuCounter = new PerformanceCounter();
                cpuCounter.CategoryName = "Processor";
                cpuCounter.CounterName = "% Processor Time";
                cpuCounter.InstanceName = "_Total";
                HttpContext.Current.Application["cpuobj"] = cpuCounter;
            }
            else
            {
                cpuCounter = HttpContext.Current.Application["cpuobj"] as PerformanceCounter;
            }
            JToken json;
            try
            {
                json = JObject.Parse("{ 'usage' : '" + HttpContext.Current.Application["cpu"] + "'}");
            }
            catch
            {
                json = JObject.Parse("{ 'usage' : '" + 0 + "'}");
            }
                HttpContext.Current.Application["cpu"] = cpuCounter.NextValue();
            var response = Request.CreateResponse(System.Net.HttpStatusCode.OK);
            response.Content = new JsonContent(json);
            return response;
        }
        catch (Exception e)
        {
            var response = Request.CreateResponse(System.Net.HttpStatusCode.BadRequest);
            JToken json = JObject.Parse("{ 'problem' : '" + e.Message + "'}");
            response.Content = new JsonContent(json);
            return response;
        }
    }
}
public class JsonContent : HttpContent { private readonly JToken _value;
    public JsonContent(JToken value)
    {
        _value = value;
        Headers.ContentType = new MediaTypeHeaderValue("application/json");
    }
    protected override Task SerializeToStreamAsync(Stream stream,
        TransportContext context)
    {
        var jw = new JsonTextWriter(new StreamWriter(stream))
        {
            Formatting = Formatting.Indented
        };
        _value.WriteTo(jw);
        jw.Flush();
        return Task.FromResult<object>(null);
    }
    protected override bool TryComputeLength(out long length)
    {
        length = -1;
        return false;
    }
}
Use:
new PerformanceCounter("Processor Information", "% Processor Time", "_Total");
Instead of:
new PerformanceCounter("Processor", "% Processor Time", "_Total");
 
    
    