Display total and free physical, virtual, and page file memory in C#

When it starts, the program uses the following code to display the system's memory information in its ListView control.

// Prepare the ListView and display values.
private void Form1_Load(object sender, EventArgs e)
{
    // Make the columns.
    lvwInfo.View = View.Details;
    lvwInfo.SetColumnHeaders(new object[]
        {
            "Property", HorizontalAlignment.Left,
            "Value", HorizontalAlignment.Right
        });

    // Add the values.
    ManagementObjectSearcher os_searcher = new ManagementObjectSearcher(
        "SELECT * FROM Win32_OperatingSystem");
    foreach (ManagementObject mobj in os_searcher.Get())
    {
        GetInfo(mobj, "FreePhysicalMemory");
        GetInfo(mobj, "FreeSpaceInPagingFiles");
        GetInfo(mobj, "FreeVirtualMemory");
        GetInfo(mobj, "SizeStoredInPagingFiles");
        GetInfo(mobj, "TotalSwapSpaceSize");
        GetInfo(mobj, "TotalVirtualMemorySize");
        GetInfo(mobj, "TotalVisibleMemorySize");
    }

    // Size the columns.
    lvwInfo.SizeColumnsToFitDataAndHeaders();
}

This code sets the ListView's View property to show details. It then calls the SetColumnHeaders extension method to make the control display two column headers: Property and Value.

Next the program creates a ManagementObjectSearcher to get information from the Win32_OperatingSystem structure. It loops through the returned searchers (there will be only one) and calls the GetInfo method to get information about several of the searcher's properties.

The code finishes by calling SizeColumnsToFitDataAndHeaders extension method to size the ListView's columns to fit its data and headers. (See Add useful extension methods to the ListView control to let it sort using all columns or clicked columns in C# for information about the ListView extension methods.)

The following code shows the GetInfo method.

// Add information about the property to the ListView.
private void GetInfo(ManagementObject mobj, string property_name)
{
    object property_obj = mobj[property_name];
    if (property_obj == null)
    {
        lvwInfo.AddRow(property_name, "???");
    }
    else
    {
        ulong property_value = (ulong)property_obj;
        lvwInfo.AddRow(property_name, property_value.ToFileSizeApi());
    }
}

This method gets the ManagementObject's property information by using the property's name as an index. If the result is null, the code adds the property name and some question marks to the ListView control. If the object is not null, the code converts it into an unsigned long and displays the property name and its value in the ListView. The code uses the ToFileSizeApi extension method to convert the long value into a memory size value such as KB, MB, GB, etc. (For information about that extension method, see Format a number of bytes in KB, MB, GB, and so forth in C#.)

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.