Issue
I run the PowerShell command to get total RAM capacity using C# and it returns this value. @{TotalPhysicalMemory= 17856325}
. I wrote a code to get the only integer value. This is the code.
string ramSize = "@{TotalPhysicalMemory= 17856325}";
string ramValue = ramSize.Split('=')[1].Split('}')[0].Trim().ToString();
This code returns the integer value. But I want to know are there any ways to get this integer value easily?
Solution
a simple regex to extract the digits in the string will do it.
var ram = new Regex(@"\d+").Match(ramSize).Value;
you can also add System.Management and directly get the size by
var ram =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT TotalPhysicalMemory FROM Win32_ComputerSystem")
.Get().OfType<ManagementObject>().First()
.Properties.OfType<PropertyData>().First().Value;
Answered By - Keith Nicholas Answer Checked By - Mary Flores (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.