By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,188 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

check RAM size and configuration?

P: n/a
Hi coders,

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

Regards,
Tarun
Oct 9 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Tarun" <Ta***@discussions.microsoft.comwrote in message
news:3B**********************************@microsof t.com...
| 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.

Oct 9 '06 #2

P: n/a
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:
>
"Tarun" <Ta***@discussions.microsoft.comwrote in message
news:3B**********************************@microsof t.com...
| 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.

Oct 9 '06 #3

P: n/a
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.

"Tarun" <Ta***@discussions.microsoft.comwrote in message
news:2F**********************************@microsof t.com...
| 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:
|
| >
| "Tarun" <Ta***@discussions.microsoft.comwrote in message
| news:3B**********************************@microsof t.com...
| | 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.
| >
| >
| >
| >
Oct 9 '06 #4

P: n/a
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.

"Tarun" <Ta***@discussions.microsoft.comwrote in message
news:2F**********************************@microsof t.com...
| 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:
|
| >
| "Tarun" <Ta***@discussions.microsoft.comwrote in message
| news:3B**********************************@microsof t.com...
| | 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.
| >
| >
| >
| >
Oct 10 '06 #5

P: n/a
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);
}
}
On Tue, 10 Oct 2006 06:01:01 +0200, Tarun
<Ta***@discussions.microsoft.comwrote:
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.

"Tarun" <Ta***@discussions.microsoft.comwrote in message
news:2F**********************************@microso ft.com...
| 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:
|
| >
| "Tarun" <Ta***@discussions.microsoft.comwrote in message
| news:3B**********************************@microsof t.com...
| | 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.
| >
| >
| >
| >


--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 10 '06 #6

P: n/a
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.

"Tarun" <Ta***@discussions.microsoft.comwrote in message
news:00**********************************@microsof t.com...
| 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.
| >
| "Tarun" <Ta***@discussions.microsoft.comwrote in message
| news:2F**********************************@microsof t.com...
| | 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:
| |
| | >
| | "Tarun" <Ta***@discussions.microsoft.comwrote in message
| | news:3B**********************************@microsof t.com...
| | | 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.
| | >
| | >
| | >
| | >
| >
| >
| >
Oct 10 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.