tags:

views:

35

answers:

2

I am querying for the current page file usage on Windows-based systems. This is meant to run on the .NET Framework, version 2.0 SP1 and has been tested (and works!) on Windows Vista, Windows 7, Windows Server 2003 R2 and Windows Server 2008.

On Windows Server 2003 SP2 (original release, not R2) it appears to return 0:

using (var query = new ManagementObjectSearcher("SELECT CurrentUsage FROM Win32_PageFileUsage"))
{
    foreach (ManagementBaseObject obj in query.Get())
    {
        uint used = (uint)obj.GetPropertyValue("CurrentUsage");
        return used;
    }
}

This is simply returning the results from the first row returned for the WMI call. Even when a page file is being used, it returns 0. What causes the result to be 0 when it should be returning a larger value?

It may be something specific to the machine in question, and could have nothing to do with the operating system version. I've also tried this with and without elevated privileges with the same results.

+1  A: 

The WMI provider is probably reading the same stat data store used by the \PagingFile\% Usage counter.

Try use PdhOpenQuery/PdhAddCounter/PdhCollectQueryData instead.

Sheng Jiang 蒋晟
Thanks for your answer. In this case, the returned value was correct and I was a bit too eager to post a question to SO. :-)
Ryan Duffield
A: 

It turns out the value actually was zero. The hosting provider in this particular instance set all paging memory to 0 MB. So, the data being returned via WMI was indeed correct.

Ryan Duffield