1

Been trying to get this work but I just couldn't get the exact solution

PerformanceCounter disk = new PerformanceCounter("LogicalDisk", "% Disk Time", @"F:");
//disk.NextValue(); <------but this doesn't give me exact Active time% 

enter image description here

3
  • 3
    learn.microsoft.com/en-us/previous-versions/aa394262(v=vs.85) Commented Mar 4, 2023 at 16:53
  • @HansPassant I tried your way to check disk usage "string query = $"SELECT * FROM Win32_PerfFormattedData_PerfDisk_LogicalDisk WHERE Name = 'F:'";" every second. Still not getting accurate it shows 212%, 320%,64%... Commented Mar 4, 2023 at 19:23
  • Nvm I had to use "PercentIdleTime" instead of "PercentDiskTime" Commented Mar 4, 2023 at 19:31

1 Answer 1

1

From the link HansPassant sent me, I developed a code and now it works perfect.

 string instanceName = "F:";
        string query = $"SELECT * FROM Win32_PerfFormattedData_PerfDisk_LogicalDisk WHERE Name = '{instanceName}'";
        foreach (ManagementBaseObject result in new ManagementObjectSearcher(query).Get())
        {
            Console.WriteLine("Disk usage: {0}%",  100-Convert.ToInt32(result["PercentIdleTime"]));
        }

Another solution to my previous code is to simply use "% Idle Time" in counterName paramter.

    PerformanceCounter counter = new PerformanceCounter("LogicalDisk", "% Idle Time", "F:");
Console.WriteLine("Disk idle time: {0}%", 100-(int)counter.NextValue());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.