-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicSystemInfo.cs
81 lines (75 loc) · 2.63 KB
/
BasicSystemInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace CompilerBenchmarker
{
class BasicSystemInfo
{
static string WaitAndRead(string exe, string args)
{
using var p = new Process();
p.StartInfo.FileName = exe;
p.StartInfo.Arguments = args;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
var sout = p.StandardOutput.ReadToEnd();
return string.IsNullOrWhiteSpace(sout)
? $"No standard output found for `{exe} {args}`"
: sout;
}
public static BasicSystemInfo Find()
{
string versCmd = "", versCmdArg = "", cpuCmd = "", cpuCmdArg = "", memCmd = "", memCmdArg = "";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
versCmd = "wmic";
versCmdArg = "os get Caption";
cpuCmd = "wmic";
cpuCmdArg = "cpu";
memCmd = "wmic";
memCmdArg = "computersystem get TotalPhysicalMemory";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
versCmd = "uname";
versCmdArg = "-r";
cpuCmd = "lscpu";
memCmd = "free";
memCmdArg = "-m";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
versCmd = "sw_vers";
cpuCmd = "sysctl";
cpuCmdArg = "-n machdep.cpu.brand_string";
memCmd = "vm_stat";
}
else
{
return new BasicSystemInfo
{
OS = "Unknown",
CPU = "Unknown",
Memory = "Unknown"
};
}
var sep = "\n-----------------------------\n";
var vers = $"\n{sep}{versCmd} {versCmdArg}{sep}" + WaitAndRead(versCmd, versCmdArg);
var cpu = $"\n{sep}{cpuCmd} {cpuCmdArg}{sep}" + WaitAndRead(cpuCmd, cpuCmdArg);
var mem = $"\n{sep}{memCmd} {memCmdArg}{sep}" + WaitAndRead(memCmd, memCmdArg);
return new BasicSystemInfo
{
OS = vers,
CPU = cpu,
Memory = mem
};
}
BasicSystemInfo()
{
}
public string OS { get; private init; }
public string CPU { get; private init; }
public string Memory { get; private init; }
}
}