关于c# 中读取系统内存大小的问题。
2014-03-06 12:44
429 查看
在程序中,调用WMI的时候,出现一个问题,就是我系统有插了两条内存条,共4G。然而自己只能在程序中查到安装内存为2G,感觉有点不淡定。这是之前的代码。
static ManagementObjectSearcher PhysicalMemory = new ManagementObjectSearcher("select * from Win32_PhysicalMemory");
// 安装内存
string hd = strInstalledMemory + String.Format("{0} GB", Convert.ToInt64(GetValue(PhysicalMemory, "Capacity")) / 1024 / 1024 / 1024);
static object GetValue(ManagementObjectSearcher searcher, string propName)
{
foreach (ManagementObject mobj in searcher.Get())
return mobj[propName];
throw new NotSupportedException();
}
这个明显是不完整的,并没有查询到所有的内存,多个内存条的话,就不行了。然后我改进了下:
//获取安装内存大小
double capacity = 0;
string hd = "";
ManagementClass cimobject1 = new ManagementClass("Win32_PhysicalMemory");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach (ManagementObject mo1 in moc1)
{
capacity += ((Math.Round(Int64.Parse(mo1.Properties["Capacity"].Value.ToString()) / 1024 / 1024 / 1024.0, 1)));
}
moc1.Dispose();
cimobject1.Dispose();
hd = "安装内存:"+ capacity + "G";
这样就搞定了。水平有限,如有更好的方法,求分享。。
static ManagementObjectSearcher PhysicalMemory = new ManagementObjectSearcher("select * from Win32_PhysicalMemory");
// 安装内存
string hd = strInstalledMemory + String.Format("{0} GB", Convert.ToInt64(GetValue(PhysicalMemory, "Capacity")) / 1024 / 1024 / 1024);
static object GetValue(ManagementObjectSearcher searcher, string propName)
{
foreach (ManagementObject mobj in searcher.Get())
return mobj[propName];
throw new NotSupportedException();
}
这个明显是不完整的,并没有查询到所有的内存,多个内存条的话,就不行了。然后我改进了下:
//获取安装内存大小
double capacity = 0;
string hd = "";
ManagementClass cimobject1 = new ManagementClass("Win32_PhysicalMemory");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach (ManagementObject mo1 in moc1)
{
capacity += ((Math.Round(Int64.Parse(mo1.Properties["Capacity"].Value.ToString()) / 1024 / 1024 / 1024.0, 1)));
}
moc1.Dispose();
cimobject1.Dispose();
hd = "安装内存:"+ capacity + "G";
这样就搞定了。水平有限,如有更好的方法,求分享。。
相关文章推荐
- 关于Poi读取Excel引发内存溢出问题的解决方法
- 关于系统首页绘制问题(ext布局+c#后台加入数据)经html输出流输出响应client
- 关于Win7 64位系统内存只显示3G的问题
- C#中出现了关于尝试读取受保护的内存,这通常指示其它内存已坏
- 关于根据指针测定一块内存所占空间大小的问题
- 关于32位windows系统支持多少内存的问题
- 关于realarm S5P4418的u-boot显示内存大小问题解决方法
- 关于 QRCode 的问题[C# 生成二维码固定大小]
- 关于android当中Service读取应用中的内存数据不更新问题
- 关于指针变量所占内存空间大小的问题
- 关于设置JVM内存大小的问题
- 关于结构体,联合体及内存大小的问题
- 关于重装系统时读取不到硬盘和设置主板AHCI蓝屏问题
- 关于windows系统内存的问题
- 关于C#调用VC编的DLL后报“System.AccessViolationException:尝试读取或写入受保护的内存
- 关于Android切换系统字体大小和字体样式导致App崩溃或者界面错乱的问题
- 关于Web系统引用js或其他文件路径读取的问题
- 关于Windows系统下,Python读取文件的问题。
- C#中关于TabControl设置选项卡的大小问题
- 关于Linux系统内存:cached过大问题,解决方案。(运维)