0

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?

5
  • 1
    It's pretty easy to pick up using a regular expression. Look up the \d character class element Commented Aug 12, 2021 at 5:35
  • i try to do, but i cant figure out how use this. can you explai? @Flydog57 Commented Aug 12, 2021 at 5:44
  • 1
    The point of PowerShell commands is that they return objects. In fact, this string you're trying to parse is a representation of an object that has a TotalPhysicalMemory property. You should request that property directly. If you're dealing with the string rendering of that object, you lost the advantage well before this point. Commented Aug 12, 2021 at 5:45
  • 1
    Get a search tool (Google, Bing), and search for my italicized term ("Regular Expression") maybe add \d to the search, and C# for good luck. My search turned up learn.microsoft.com/en-us/dotnet/standard/base-types/…, a very good link. Then in the left hand pain, there's a link to the "Overview" topic: learn.microsoft.com/en-us/dotnet/standard/base-types/…. Between the two, you should be able to figure things out. Pulling a number from a string is one of the basic RegEx examples. Enjoy learning Commented Aug 12, 2021 at 14:12
  • 1
    One thing to note is that if you are looking at total RAM for a computer, you probably don't want to use an int. Use a long instead. Commented Aug 12, 2021 at 14:18

3 Answers 3

3

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;
Sign up to request clarification or add additional context in comments.

Comments

3

To only get the first Number in a string you can easily do this:

using System.Text.RegularExpressions 
string ramValue = Regex.Match(ramSize, @"\d+").Value;

To get all numbers in a string you could use Linq:

using System.Linq
string ramValue = ramSize.Where(Char.IsDigit).ToArray()

also have a look over here:

Find and extract a number from a string

Comments

3

You can use Regex to get the value.

Regex 101

First capturing group

\d matches a digit (equivalent to [0-9]) and + matches the previous token between one and unlimited times

Hence, match.Groups[1].Value will get the value from group (\d+) as mentioned above.

using System.Text.RegularExpression;

string ramSize = "@{TotalPhysicalMemory= 17856325}";
Regex regex = new Regex(@"^\@{TotalPhysicalMemory= (\d+)}");
var match = regex.Match(str);
var ramValue = match.Groups[1].Value;

Console.WriteLine("Value: " + ramValue);

Output

Value: 17856325

Sample program

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.