读取远程计算机上的内存量时出现问题
我正在尝试确定计算机中安装的物理内存量。为了完成此任务,我使用 WMI(通过 .net 4.0)及其服务。问题是,无论远程计算机有多少内存,返回的值都是 4GB。这已经在三台远程计算机上进行了测试:
- 虚拟机,1GB RAM,Windows 2003
- 物理机,2GB RAM,Windows XP
- 物理机,2GB RAM,Windows 7 64位
我自己正在运行物理机,4GB RAM,Windows 7 64位。
显示代码:
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
并从 Win32_OperatingSystem 选择 TotalVisibleMemorySize
作为查询,但无济于事。最后 phisicalMemorySize
将始终为 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:
- Virtual machine, 1GB RAM, Windows 2003
- Physical machine, 2GB RAM, Windows XP
- Physical machine, 2GB RAM, Windows 7 64bit
I myself am running physical machine, 4GB RAM, Windows 7 64bit.
Showing the code:
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);
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TotalPhysicalMemory 在 MSDN 库文档中有一个响亮的免责声明:
Afaik,所有现代机器都会将其 BIOS 复制到内存中。我认为容量属性是机器中可用的内存量,而不是现有的内存量。在任何 32 位操作系统上,这都是 2 GB;在 64 位操作系统上,32 位进程则为 4 GB。例如,与 List<>.Capacity 与 Count 属性进行比较。
我得到了一个不错的 TotalPhys 价值,3 GB,我知道我的笔记本电脑上有这个。容量为2GB,与操作系统匹配。 WMI 有时确实会变得不稳定,它并不完美。
使用 WMI 代码创建器实用程序以获得第二意见。我认为它是一个 .NET 1.1 程序,因此如果您在 64 位操作系统上运行它,请注意它的结果。如果您使用 Visual Studio 2010,请注意项目中的平台目标设置。它默认为 x86,因此即使在 64 位操作系统上,您也可以在 32 位模式下运行。项目 + 属性、构建选项卡、平台目标设置。
TotalPhysicalMemory has a loud disclaimer in the MSDN Library docs:
Afaik, all modern machines copy their BIOS to memory. I think the Capacity property is how much memory is usable in the machine, not how much is present. Which is 2 gigabytes on any 32-bit operating system, 4 gigabytes for a 32-bit process on a 64-bit operating system. Compare to, say, the List<>.Capacity vs Count property.
I'm getting a decent value for TotalPhys, 3 gigabytes which I know I have on my laptop. Capacity is 2 gigabytes, matches the operating system. WMI does get flaky sometimes, it is hardly perfect.
Use the WMI Code Creator utility to get a second opinion. I think it is a .NET 1.1 program so beware of its results if you run it against a 64-bit operating system. If you use Visual Studio 2010 then watch out for the Platform Target setting in your project. It defaults to x86 so you'll run in 32-bit mode, even on a 64-bit operating system. Project + Properties, Build tab, Platform target setting.
发现问题了。是用
线来的。正确的应该是
它看起来默认为本地计算机。
感谢 Hans 向我推荐了 WMI Code Creator。这个工具确实帮了很大的忙。
Found the problem. It is with the
line. The correct would be
It looks like it defaulted to the local computer.
Thanks to Hans who pointed me to WMI Code Creator. That tool realy helped a great deal.