读取远程计算机上的内存量时出现问题 [英] Problem reading amount of memory on remote computer
问题描述
我正在尝试确定计算机中安装的物理内存量.为了实现这一点,我正在使用 WMI(通过 .net 4.0)及其服务.问题是无论远程计算机有多少内存,返回的值都是 4GB.这已经在三台远程计算机上进行了测试:
I am trying to determine an amount of physical memory installed in a computer. To acomplish this I am using WMI (through .net 4.0) and it's services. The problem is that no matter what amount of memory remote computer has, value returned is 4GB. This has been tested with three remote computers:
- 虚拟机,1GB RAM,Windows 2003
- 物理机,2GB RAM,Windows XP
- 实体机,2GB RAM,Windows 7 64 位
我自己正在运行物理机,4GB RAM,Windows 7 64 位.
I myself am running physical machine, 4GB RAM, Windows 7 64bit.
显示代码:
uint phisicalMemorySize = 0;
ConnectionOptions co = new ConnectionOptions();
co.Username = null;
ManagementScope ms = new ManagementScope("\\\\" + computerName, co);
ObjectQuery q = new ObjectQuery("select TotalPhysicalMemory from Win32_ComputerSystem");
ManagementObjectSearcher os = new ManagementObjectSearcher(ms, q);
ManagementObjectCollection moc = os.Get();
foreach (ManagementObject o in moc)
{
phisicalMemorySize += Convert.ToUInt64(o["TotalPhysicalMemory"], CultureInfo.InvariantCulture);
}
我也尝试过使用 select Capacity from Win32_PhysicalMemory
并 select TotalVisibleMemorySize from Win32_OperatingSystem
作为查询,但无济于事.最后 phisicalMemorySize
总是 4GB.
I have also tried using select Capacity from Win32_PhysicalMemory
and select TotalVisibleMemorySize from Win32_OperatingSystem
as queries but to no avail. At the end phisicalMemorySize
would allways be 4GB.
推荐答案
发现问题.它与
ManagementScope ms = new ManagementScope("\\\\" + computerName, co);
线.正确的应该是
ManagementScope ms = new ManagementScope("\\\\" + computerName + "\\root\\CIMV2", co);
看起来它默认为本地计算机.
It looks like it defaulted to the local computer.
感谢 Hans 向我指出 WMI Code Creator.该工具确实帮了大忙.
Thanks to Hans who pointed me to WMI Code Creator. That tool realy helped a great deal.
这篇关于读取远程计算机上的内存量时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!