get total memory (ram) pc in c# [closed]

▼魔方 西西 提交于 2019-12-13 11:23:16

问题


hi edit this code but get error " Cannt apply indexing with [] to an expression of type "view_process.managmentobject" for line 8

and what is formul for get 30% my memory?

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:


Add a reference to Microsoft.VisualBasic.dll. Then getting total physical memory is as simple as this (yes, I tested it):

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


来源:https://stackoverflow.com/questions/15790706/get-total-memory-ram-pc-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!