in C# how do i get to total number of kilobytes of memroy total and available for the current machin

D

Daniel

in C# how do i get to total number of kilobytes of memroy total and
available for the current machine?
 
W

Wessel Troost

in C# how do i get to total number of kilobytes of memroy total and
available for the current machine?
This seems to work:

ManagementScope scope = new ManagementScope("\\root\\cimv2");
scope.Connect();

ObjectQuery query = new ObjectQuery("SELECT * FROM
Win32_OperatingSystem");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(scope, query);

ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
Console.WriteLine("Available memory: {0}",
m["FreePhysicalMemory"]);
}

query = new ObjectQuery("SELECT * FROM Win32_ComputerSystem");
searcher = new ManagementObjectSearcher(scope, query);
queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
Console.WriteLine("Total memory: {0}",
m["TotalPhysicalMemory"]);
}

Greetings,
Wessel
 
S

SevDer

Another approach will be usage of performance counters..

--

SevDer
http://www.sevder.com
A new .NET Source For .NET Developers
Wessel Troost said:
in C# how do i get to total number of kilobytes of memroy total and
available for the current machine?
This seems to work:

ManagementScope scope = new ManagementScope("\\root\\cimv2");
scope.Connect();

ObjectQuery query = new ObjectQuery("SELECT * FROM
Win32_OperatingSystem");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(scope, query);

ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
Console.WriteLine("Available memory: {0}",
m["FreePhysicalMemory"]);
}

query = new ObjectQuery("SELECT * FROM Win32_ComputerSystem");
searcher = new ManagementObjectSearcher(scope, query);
queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
Console.WriteLine("Total memory: {0}",
m["TotalPhysicalMemory"]);
}

Greetings,
Wessel
 
W

Willy Denoyette [MVP]

SevDer said:
Another approach will be usage of performance counters..

It's not possible to get the total system memory (RAM) from perfcounters,
available memory is no problem though.

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

Top