Search code examples
c#performancecounter

PerformanceCounter PhysicalDisk % Disk Time wrong value


private static PerformanceCounter PC18 = new PerformanceCounter("PhysicalDisk", "% Disk Time", "_Total");

Calling the Counter using:

private void Timer_Tick(object sender, EventArgs e)
{
    Console.WriteLine($"PC18.NextValue()");
}

This Timer, is set to 1 second intervals.

I am getting values that have no corresponding Disk Usage %, values like 130.02 and so on are being displayed.

This is the only PerformanceCounter I can find that does Disk Usage. Why am I getting the wrong values?


Solution

  • Ah ha, the instanceName, '_total' is not the figure on of disk, its all disks.

    So, an answer here fixed my problem:

    PC18 = new PerformanceCounter("PhysicalDisk", "% Disk Time", "0 C:");
    

    Where: '0 C:' is the Instance Name of my physical disk C:

    Thanks to 'Henk Holterman', all though the answer was not entirely clear, it was enough to make towards the final answer. Thanks!

    PerformanceCounterCategory Category = new System.Diagnostics.PerformanceCounterCategory("PhysicalDisk");
    string[] InstanceNames = Category.GetInstanceNames();
    
    foreach (string Name in InstanceNames)
        richTextBox1.AppendText(Name + Environment.NewLine);
    

    That will give you the Instance Names you need.