Skip to content
This repository has been archived by the owner on Feb 24, 2022. It is now read-only.

[Suggestion] Alternative way to pull hardware, software and other system information #8

Open
johngagefaulkner opened this issue May 25, 2021 · 0 comments

Comments

@johngagefaulkner
Copy link

johngagefaulkner commented May 25, 2021

The way that you're going about pulling Hardware, Software and other System Information right now is causing you to have a lot of unnecessary code. I can also tell, by the comments you've written, that you don't know what a lot of the offsets mean.

There's a pretty easy way to handle pulling information about the Operating System, RAM, CPU, HDDs, etc.

First off, here's some code I wrote to easily run a PowerShell command from C# and have the output redirected to you ─

public class PowerShell
        {
            public static string ExecuteCommand(string command)
            {
                try
                {
                    Process _powerShell = new Process();
                    _powerShell.StartInfo.UseShellExecute = false;
                    _powerShell.StartInfo.RedirectStandardOutput = true;
                    _powerShell.StartInfo.CreateNoWindow = true;
                    _powerShell.StartInfo.Verb = "runas";
                    _powerShell.StartInfo.FileName = "powershell.exe";
                    _powerShell.StartInfo.Arguments = $"-NoProfile -ExecutionPolicy Bypass -NoLogo -Command {command}";
                    _powerShell.Start();

                    string _output = _powerShell.StandardOutput.ReadToEnd();
                    _powerShell.WaitForExit();

                    return _output.ToString().Trim();
                }

                catch (Exception ex)
                {
                    return "Error: " + ex.Message;
                }
            }
        }

Next, here's an example of a command I wrote to pull the Manufacturer, Part Number, Capacity and Speed of all installed RAM sticks. It then converts them to JSON which you can very easily parse in C# ─

Get-CimInstance -ClassName CIM_Chip |Select-Object Manufacturer,PartNumber,Capacity,Speed -ExcludeProperty "CIM*" |ConvertTo-Json

Here's an example of the output ─

[
    {
        "Manufacturer":  "Corsair",
        "PartNumber":  "CMK16GX4M2D3200C16  ",
        "Capacity":  8589934592,
        "Speed":  3200
    },
    {
        "Manufacturer":  "Corsair",
        "PartNumber":  "CMK16GX4M2D3200C16  ",
        "Capacity":  8589934592,
        "Speed":  3200
    }
]

Here's an example C# class that you could use ─

public class RAM
{
    public string Manufacturer { get; set; }
    public string PartNumber { get; set; }
    public int Capacity { get; set; }
    public int Speed { get; set; }

    // THIS WILL NOT WORK UNLESS YOU UPGRADE FROM .NET FRAMEWORK 4.8 TO .NET 5.0
    public static RAM GetInfo()
    {
        string _cim = "Get-CimInstance -ClassName CIM_Chip |Select-Object Manufacturer,PartNumber,Capacity,Speed -ExcludeProperty 'CIM*' |ConvertTo-Json";
        string json = PowerShell.ExecuteCommand(_cim);
        return JsonSerializer.Deserialize<RAM[]>(json);
}

The usage would be something like this ─

public void form1_loaded()
{
    int _totalRAM;
    var _RAM = RAM.GetInfo();
    foreach (var _stick in _RAM)
    {
        _totalRAM = _totalRAM + _stick.Capacity;
    }

    var _KB = _totalRAM / 1024;
    var _MB = _KB / 1024;
    var _GB = _MB / 1024;

    MessageBox.Show("You have " + _GB.ToString() + "GB RAM installed!", "Information");
}

I hope this helps! Let me know if you need any further assistance with it. I've attached a list of all the CIM classes you can use (there are a lot...) It's a CSV file but GitHub wouldn't let me attach it with that extension, so just rename it to .csv and you'll be good to go.
CimClasses-CSV.txt

EDIT: Link to some CimInstance documentation by Microsoft: https://docs.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance?view=powershell-7.1#examples

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant