-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathHardwareInfo.cs
More file actions
104 lines (95 loc) · 2.69 KB
/
HardwareInfo.cs
File metadata and controls
104 lines (95 loc) · 2.69 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using BytecodeApi.Extensions;
using BytecodeApi.Wmi;
using Microsoft.Win32;
using System.Runtime.InteropServices;
namespace BytecodeApi.Win32.SystemInfo;
/// <summary>
/// Provides information about installed hardware.
/// </summary>
public static class HardwareInfo
{
private static long? _TotalMemory;
/// <summary>
/// Gets the names of all installed processors.
/// </summary>
public static string[] ProcessorNames
{
get
{
if (field == null)
{
List<string> names = [];
using RegistryKey key = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\CentralProcessor") ?? throw Throw.Win32();
foreach (string subKeyName in key.GetSubKeyNames())
{
using RegistryKey subKey = key.OpenSubKey(subKeyName) ?? throw Throw.Win32();
if (subKey.GetStringValue("ProcessorNameString")?.Trim().ToNullIfEmpty() is string name)
{
names.Add(name);
}
}
field = names.ToArray();
}
return field;
}
}
/// <summary>
/// Gets the names of all installed video controllers.
/// </summary>
public static string[] VideoControllerNames => field ??= WmiContext.Root
.GetNamespace("CIMV2")
.GetClass("Win32_VideoController")
.Select("Name")
.ToArray()
.Select(obj => obj.Properties["Name"].GetValue<string>()?.Trim().ToNullIfEmpty())
.ExceptNull()
.ToArray();
/// <summary>
/// Gets the total amount of installed physical memory.
/// </summary>
public static long TotalMemory
{
get
{
if (_TotalMemory == null)
{
Native.MemoryStatusEx memoryStatus = new();
_TotalMemory = Native.GlobalMemoryStatusEx(memoryStatus) ? (long)memoryStatus.TotalPhys : throw Throw.Win32();
}
return _TotalMemory.Value;
}
}
/// <summary>
/// Gets the total amount of available physical memory.
/// </summary>
public static long AvailableMemory
{
get
{
Native.MemoryStatusEx memoryStatus = new();
return Native.GlobalMemoryStatusEx(memoryStatus) ? (long)memoryStatus.AvailPhys : throw Throw.Win32();
}
}
}
file static class Native
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool GlobalMemoryStatusEx([In, Out] MemoryStatusEx buffer);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public sealed class MemoryStatusEx
{
public uint Length;
public uint MemoryLoad;
public ulong TotalPhys;
public ulong AvailPhys;
public ulong TotalPageFile;
public ulong AvailPageFile;
public ulong TotalVirtual;
public ulong AvailVirtual;
public ulong AvailExtendedVirtual;
public MemoryStatusEx()
{
Length = (uint)Marshal.SizeOf<MemoryStatusEx>();
}
}
}