Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
826 views
in Technique[技术] by (71.8m points)

c# - WMI hardware, get RAM info

I need to retrieve RAM info from both local and remote computers, I am aware of WMI in System.Management and I am using it, but my problem with WMI is that the Win32_PhysicalMemory class has a value that I need called "MemoryType", but it always returns 0 or "Unknown".
Win32_PhysicalMemory class (http://msdn.microsoft.com/en-us/library/aa394347%28v=vs.85%29.aspx)

I have tried to use Win32_PhysicalMemory from both C# and VBScript on 3 different XP Professional computers with an admin account and got the same 0 or "Unknown" MemoryType value returned. The code I used is simple and short, copy and pasted from a number of sources around the net so I'm sure there aren't major problems with it.

Am I using WMI wrongly or is there a Windows API alternative i can use?
Remote reports aren't essential.

Specifically I need to count the number of sticks of RAM it has, or can have, the speed, and the type of RAM it uses, DDR2, DDR3, etc., the Win32_PhysicalMemory class gives me all this except the type of RAM.

ConnectionOptions connection = new ConnectionOptions();
connection.Impersonation = ImpersonationLevel.Impersonate;

ManagementScope scope = new ManagementScope("\\.\root\CIMV2", connection);                
scope.Connect();

ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_PhysicalMemory");

ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

foreach (ManagementObject queryObj in searcher.Get())
{
    System.Diagnostics.Debug.WriteLine("-----------------------------------");
    System.Diagnostics.Debug.WriteLine("Capacity: {0}", queryObj["Capacity"]);
    System.Diagnostics.Debug.WriteLine("MemoryType: {0}", queryObj["MemoryType"]);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to this kb article, certain types of memory will be listed as unknown since it wasn't in the SMBIOS (which WMI uses) at the time. Apparently it hasn't been updated since then. It says it applies to Windows Server 2003 but I see the same results on Windows 7 x64.

I suppose to get around this, you can cut the middle man and not use WMI but use the SMBIOS directly. I won't be of much help there but at least it will give you a direction to go on.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...