在c#中获取总内存(ram)pc

时间:2013-04-03 14:51:46

标签: c# ram

您可以编辑此代码,但会收到错误“无法将带有[]的索引应用于第8行类型为”view_process.managmentobject“的表达式

什么是为了获得30%的记忆?

private static void DisplayTotalRam()
    {
        string Query = "SELECT MaxCapacity FROM Win32_PhysicalMemoryArray";
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(Query);
        int amount = 0;
        foreach (ManagementObject memo in searcher.Get())
        {
            amount += Convert.ToInt32(Convert.ToInt64(memo["Capacity"]) / 1024 / 1024 / 1024);

        }
    }

1 个答案:

答案 0 :(得分:2)

添加对Microsoft.VisualBasic.dll的引用。然后获得总物理内存就像这样简单(是的,我测试过它):

static ulong GetTotalMemoryInBytes()
{
    return new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory;
}