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?

Yong Shun
  • 35,286
  • 4
  • 24
  • 46
wisnshaftler
  • 91
  • 1
  • 10
  • 1
    It's pretty easy to pick up using a _regular expression_. Look up the `\d` character class element – Flydog57 Aug 12 '21 at 05:35
  • i try to do, but i cant figure out how use this. can you explai? @Flydog57 – wisnshaftler Aug 12 '21 at 05: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. – madreflection Aug 12 '21 at 05: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 https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference, a very good link. Then in the left hand pain, there's a link to the "Overview" topic: https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expressions. 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 – Flydog57 Aug 12 '21 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. – Flydog57 Aug 12 '21 at 14:18

3 Answers3

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

Yong Shun
  • 35,286
  • 4
  • 24
  • 46
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;
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
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

Flo
  • 448
  • 2
  • 6