Question

In my application, I am gathering data regarding the performance of system, where I need to find

  • % Free Space
  • % Disk Time
  • % Disk Read Time
  • % Disk Write Time
  • % Idle Time
  • % Usage
  • % Usage Peak

using below function;

private void CollectnPopulatePerfCounters()
    {
        try
        {
            foreach (var pc in System.Diagnostics.PerformanceCounterCategory.GetCategories())
            {
                if (pc.CategoryName == "LogicalDisk" || pc.CategoryName == "Paging File" || pc.CategoryName == "ProcessorPerformance")
                {
                    try
                    {
                        foreach (var insta in pc.GetInstanceNames())
                        {
                            try
                            {
                                foreach (PerformanceCounter cntr in pc.GetCounters(insta))
                                {
                                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\amit.txt", true))
                                    {
                                        sw.WriteLine("--------------------------------------------------------------------");
                                        sw.WriteLine("Category Name : " + pc.CategoryName);
                                        sw.WriteLine("Counter Name : " + cntr.CounterName);
                                        sw.WriteLine("Explain Text : " + cntr.CounterHelp);
                                        sw.WriteLine("Instance Name: " + cntr.InstanceName);
                                        sw.WriteLine("Value : " + Convert.ToString(cntr.RawValue));  //TODO:
                                        sw.WriteLine("Counter Type : " + cntr.CounterType);
                                        sw.WriteLine("--------------------------------------------------------------------");
                                    }
                                }
                            }
                            catch (Exception) { }
                        }
                    }
                    catch (Exception) { }
                }
            }
        }
        catch (Exception) { }
    }

When the code is executed the data is generated. While observing I found that the value against the above mentioned list [i.e. % free space, % disk time etc.] is not in correct form.

On my machine the value for

  • % Disk Read Time = 44553438000 for C Drive
  • % Usage Peak = 48386 for \??\C:\pagefile.sys

actually the value should be in the percent form [i.e within the range of 0 to 100 %]

Is there any way to get the exact value for all these except [% free Space for which I have calculated].

Or

Does anyone know how to calculate for rest of all headers.

Was it helpful?

Solution

Use following

sw.WriteLine("Value : " + Convert.ToString(Math.Round(cntr.NextValue(),2)) + "%");

More info at:

Why the cpu performance counter kept reporting 0% cpu usage?

All the best!

Don't forget to vote :-D

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top