check RAM size and configuration?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi coders,

How can check the total RAM size and the number of RAM modules and their
respective sizes using C#?

Regards,
Tarun
 
| Hi coders,
|
| How can check the total RAM size and the number of RAM modules and their
| respective sizes using C#?
|
| Regards,
| Tarun

Query the WMI classes Win32_PhyscalMemory and Win32_MemoryDevice using
System.Management namespace classes.

Willy.
 
Thanks for your reply. I tried using the following code, querying the Win32
class from C# code:-

ManagementClass myRAM = new ManagementClass("Win32_PhysicalMemoryArray");
myRAM.Get();
Console.WriteLine("Total Physical Memory: " +
myRAM.GetPropertyValue("MaxCapacity"));

However, this does not show the memory size. In fact, it does not give any
output. But another query - myRAM.Properties.Count works absolutely fine,
returning 27.

Kindly help me with this.

Regards,
Tarun
 
Where did you read Win32_PhysicalMemoryArray? You need to query the
instances of Win32_PhysicalMemory and add the Capacity property value to get
the total RAM. If you need to obtain the memory bank occupation, you'll have
to retrieve the Win32_MemoryDevice instances and fetch the beginaddress and
eningaddress properties of each instance, if the endingaddress is 0, the
bank is free, else it's occupied.


Willy.

| Thanks for your reply. I tried using the following code, querying the
Win32
| class from C# code:-
|
| ManagementClass myRAM = new ManagementClass("Win32_PhysicalMemoryArray");
| myRAM.Get();
| Console.WriteLine("Total Physical Memory: " +
| myRAM.GetPropertyValue("MaxCapacity"));
|
| However, this does not show the memory size. In fact, it does not give any
| output. But another query - myRAM.Properties.Count works absolutely fine,
| returning 27.
|
| Kindly help me with this.
|
| Regards,
| Tarun
|
| "Willy Denoyette [MVP]" wrote:
|
| >
| > | > | Hi coders,
| > |
| > | How can check the total RAM size and the number of RAM modules and
their
| > | respective sizes using C#?
| > |
| > | Regards,
| > | Tarun
| >
| > Query the WMI classes Win32_PhyscalMemory and Win32_MemoryDevice using
| > System.Management namespace classes.
| >
| > Willy.
| >
| >
| >
| >
 
Hi Willy,

Thanks a lot. You really guided me in the right direction. I wrote the
following but I still don't get any output.

using System;
using System.Management;

namespace MemoryTest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Test
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Test t1 = new Test();
t1.checkRAM();
Console.Read();
}

void checkRAM ()
{
ManagementClass myRAM = new ManagementClass("Win32_PhysicalMemory");
myRAM.Get();
Console.Write(myRAM.GetPropertyValue("Capacity"));
}
}
}

Regards,
Tarun
 
Hi Tarun,

Not sure why your approach does not return any values, nor does it return
anything on my system.
However, there is another somewhat more complicated way which I suspect
Willy was thinking of, where you query the system tables

The code is like this

static void Main(string[] args)
{
ManagementScope scope = new ManagementScope();
ObjectQuery query = new ObjectQuery("SELECT * FROM
Win32_PhysicalMemory");

ManagementObjectSearcher searcher = new
ManagementObjectSearcher(scope, query);
ManagementObjectCollection coll = searcher.Get();

foreach (ManagementObject mo in coll)
{
Console.WriteLine("Capacity = "
+ mo.Properties["Capacity"].Value);
}
}


Hi Willy,

Thanks a lot. You really guided me in the right direction. I wrote the
following but I still don't get any output.

using System;
using System.Management;

namespace MemoryTest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Test
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Test t1 = new Test();
t1.checkRAM();
Console.Read();
}

void checkRAM ()
{
ManagementClass myRAM = new ManagementClass("Win32_PhysicalMemory");
myRAM.Get();
Console.Write(myRAM.GetPropertyValue("Capacity"));
}
}
}

Regards,
Tarun



Willy Denoyette said:
Where did you read Win32_PhysicalMemoryArray? You need to query the
instances of Win32_PhysicalMemory and add the Capacity property value
to get
the total RAM. If you need to obtain the memory bank occupation, you'll
have
to retrieve the Win32_MemoryDevice instances and fetch the beginaddress
and
eningaddress properties of each instance, if the endingaddress is 0, the
bank is free, else it's occupied.


Willy.

| Thanks for your reply. I tried using the following code, querying the
Win32
| class from C# code:-
|
| ManagementClass myRAM = new
ManagementClass("Win32_PhysicalMemoryArray");
| myRAM.Get();
| Console.WriteLine("Total Physical Memory: " +
| myRAM.GetPropertyValue("MaxCapacity"));
|
| However, this does not show the memory size. In fact, it does not
give any
| output. But another query - myRAM.Properties.Count works absolutely
fine,
| returning 27.
|
| Kindly help me with this.
|
| Regards,
| Tarun
|
| "Willy Denoyette [MVP]" wrote:
|
| >
| > | > | Hi coders,
| > |
| > | How can check the total RAM size and the number of RAM modules and
their
| > | respective sizes using C#?
| > |
| > | Regards,
| > | Tarun
| >
| > Query the WMI classes Win32_PhyscalMemory and Win32_MemoryDevice
using
| > System.Management namespace classes.
| >
| > Willy.
| >
| >
| >
| >
 
You have to query the "instances" of these classes and enumerate their
properties, what you do is creating a class, but classes don't hold any
instance values, just take a look at Morten's post for a sample.
It's fundamental you understand the difference between a class and an
instance of a class, therefore I would suggest you to take a look at the WMI
doc's before you waste any more time with this. An excellent tool to help
you understand WMI is wbemtest.exe, give it a try.


Willy.

| Hi Willy,
|
| Thanks a lot. You really guided me in the right direction. I wrote the
| following but I still don't get any output.
|
| using System;
| using System.Management;
|
| namespace MemoryTest
| {
| /// <summary>
| /// Summary description for Class1.
| /// </summary>
| class Test
| {
| /// <summary>
| /// The main entry point for the application.
| /// </summary>
| [STAThread]
| static void Main(string[] args)
| {
| //
| // TODO: Add code to start application here
| //
| Test t1 = new Test();
| t1.checkRAM();
| Console.Read();
| }
|
| void checkRAM ()
| {
| ManagementClass myRAM = new ManagementClass("Win32_PhysicalMemory");
| myRAM.Get();
| Console.Write(myRAM.GetPropertyValue("Capacity"));
| }
| }
| }
|
| Regards,
| Tarun
|
|
|
| "Willy Denoyette [MVP]" wrote:
|
| > Where did you read Win32_PhysicalMemoryArray? You need to query the
| > instances of Win32_PhysicalMemory and add the Capacity property value to
get
| > the total RAM. If you need to obtain the memory bank occupation, you'll
have
| > to retrieve the Win32_MemoryDevice instances and fetch the beginaddress
and
| > eningaddress properties of each instance, if the endingaddress is 0, the
| > bank is free, else it's occupied.
| >
| >
| > Willy.
| >
| > | > | Thanks for your reply. I tried using the following code, querying the
| > Win32
| > | class from C# code:-
| > |
| > | ManagementClass myRAM = new
ManagementClass("Win32_PhysicalMemoryArray");
| > | myRAM.Get();
| > | Console.WriteLine("Total Physical Memory: " +
| > | myRAM.GetPropertyValue("MaxCapacity"));
| > |
| > | However, this does not show the memory size. In fact, it does not give
any
| > | output. But another query - myRAM.Properties.Count works absolutely
fine,
| > | returning 27.
| > |
| > | Kindly help me with this.
| > |
| > | Regards,
| > | Tarun
| > |
| > | "Willy Denoyette [MVP]" wrote:
| > |
| > | >
| > | > | > | > | Hi coders,
| > | > |
| > | > | How can check the total RAM size and the number of RAM modules and
| > their
| > | > | respective sizes using C#?
| > | > |
| > | > | Regards,
| > | > | Tarun
| > | >
| > | > Query the WMI classes Win32_PhyscalMemory and Win32_MemoryDevice
using
| > | > System.Management namespace classes.
| > | >
| > | > Willy.
| > | >
| > | >
| > | >
| > | >
| >
| >
| >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top