Skip to content

Commit 1ed4d82

Browse files
committed
Ram utilization methods added and added code document(ation) comments
1 parent 73a7563 commit 1ed4d82

File tree

8 files changed

+141
-12
lines changed

8 files changed

+141
-12
lines changed

PLP-SystemInfo/ComponentInfo/BoardInfo.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ namespace PLP_SystemInfo.ComponentInfo
55
{
66
public class BoardInfo
77
{
8-
//Motherboard
8+
/// <summary>
9+
/// Returns an object of type **Board** with manufacturer and model.
10+
/// </summary>
11+
/// <returns></returns>
912
public static Board GetMotherboard()
1013
{
1114
string manufacturer = "";
@@ -21,6 +24,10 @@ public static Board GetMotherboard()
2124
return new Board(manufacturer, model);
2225
}
2326

27+
/// <summary>
28+
/// Returns an object of type **BIOS** with manufacturer and version.
29+
/// </summary>
30+
/// <returns></returns>
2431
public static BIOS GetBIOSInfo()
2532
{
2633
string manufacturer = "";

PLP-SystemInfo/ComponentInfo/GraphicsInfo.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ namespace PLP_SystemInfo.ComponentInfo
66
{
77
public class GraphicsInfo
88
{
9-
//Graphics
9+
/// <summary>
10+
/// Returns a collection of type **GraphicsCard** containing information such as name and driver version.
11+
/// </summary>
12+
/// <returns></returns>
1013
public static GraphicsCollection GetGraphicscardInfo()
1114
{
1215
GraphicsCollection graphics = new GraphicsCollection();

PLP-SystemInfo/ComponentInfo/OSInfo.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ namespace PLP_SystemInfo.ComponentInfo
44
{
55
public class OSInfo
66
{
7+
/// <summary>
8+
/// Returns a string with OS name and architecture.
9+
/// </summary>
10+
/// <returns></returns>
711
public static string GetOperatingSystemInfo()
812
{
913
string osa = "";

PLP-SystemInfo/ComponentInfo/ProcessorInfo.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ static Collection<double> GetCacheSize()
4646
return result;
4747
}
4848

49+
/// <summary>
50+
/// Returns a collection of type **Processor** containing information such as name, architecture, cores, threads, cache and clock speed.
51+
/// </summary>
52+
/// <returns></returns>
4953
public static ProcessorCollection GetProcessors()
5054
{
5155
ProcessorCollection processors = new ProcessorCollection();

PLP-SystemInfo/ComponentInfo/RamInfo.cs

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,59 @@
11
using PLP_SystemInfo.Collections;
22
using System;
33
using System.Management;
4+
using System.Runtime.InteropServices;
45

56
namespace PLP_SystemInfo.ComponentInfo
67
{
78
public class RamInfo
89
{
910
//Ram
11+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
12+
private class MEMORYSTATUSEX
13+
{
14+
public uint dwLength;
15+
public uint dwMemoryLoad;
16+
public ulong ullTotalPhys;
17+
public ulong ullAvailPhys;
18+
public ulong ullTotalPageFile;
19+
public ulong ullAvailPageFile;
20+
public ulong ullTotalVirtual;
21+
public ulong ullAvailVirtual;
22+
public ulong ullAvailExtendedVirtual;
23+
24+
public MEMORYSTATUSEX()
25+
{
26+
dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));
27+
}
28+
}
29+
30+
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
31+
[return: MarshalAs(UnmanagedType.Bool)]
32+
private static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);
33+
34+
private static double ConvertBytesToGB(ulong bytes)
35+
{
36+
return Math.Round(bytes / (1024.0 * 1024.0 * 1024.0), 3);
37+
}
38+
39+
private static void GetMemoryStatus(out double totalMemoryGB, out double availableMemoryGB)
40+
{
41+
var memoryStatus = new MEMORYSTATUSEX();
42+
if (GlobalMemoryStatusEx(memoryStatus))
43+
{
44+
totalMemoryGB = ConvertBytesToGB(memoryStatus.ullTotalPhys);
45+
availableMemoryGB = ConvertBytesToGB(memoryStatus.ullAvailPhys);
46+
}
47+
else
48+
{
49+
throw new InvalidOperationException("Failed to retrieve memory status.");
50+
}
51+
}
52+
53+
/// <summary>
54+
/// Returns a long value of the installed GB of RAM.
55+
/// </summary>
56+
/// <returns></returns>
1057
public static long GetInstalledRAMSize()
1158
{
1259
var oMs = new ManagementScope();
@@ -29,21 +76,52 @@ public static long GetInstalledRAMSize()
2976
return memSize;
3077
}
3178

32-
public static long GetTotalUsableRam()
79+
/// <summary>
80+
/// Returns the total useable ram in GB.
81+
/// </summary>
82+
/// <returns></returns>
83+
public static double GetTotalUsableRam()
84+
{
85+
double memTotal, memAvailable;
86+
GetMemoryStatus(out memTotal, out memAvailable);
87+
return memTotal;
88+
}
89+
90+
/// <summary>
91+
/// Returns the harware reserved ram in MB.
92+
/// </summary>
93+
/// <returns></returns>
94+
public static double GetHardwareReservedRam()
3395
{
34-
throw new NotImplementedException();
96+
return Math.Round((GetInstalledRAMSize() - GetTotalUsableRam()) * 1000);
3597
}
3698

37-
public static long GetRamInUse()
99+
/// <summary>
100+
/// Returns the used ram in GB.
101+
/// </summary>
102+
/// <returns></returns>
103+
public static double GetRamInUse()
38104
{
39-
throw new NotImplementedException();
105+
double memTotal, memAvailable;
106+
RamInfo.GetMemoryStatus(out memTotal, out memAvailable);
107+
return memTotal - memAvailable;
40108
}
41109

42-
public static long GetAvailableRam()
110+
/// <summary>
111+
/// Returns the available ram.
112+
/// </summary>
113+
/// <returns></returns>
114+
public static double GetAvailableRam()
43115
{
44-
throw new NotImplementedException();
116+
double memTotal, memAvailable;
117+
RamInfo.GetMemoryStatus(out memTotal, out memAvailable);
118+
return memAvailable;
45119
}
46120

121+
/// <summary>
122+
/// Returns a collection of type **RAM** containing information for each installed ram module such as manufacturer, frequency, voltage and capacity.
123+
/// </summary>
124+
/// <returns></returns>
47125
public static RamCollection GetRamInfo()
48126
{
49127
RamCollection rams = new RamCollection();

PLP-SystemInfo/PLP-SystemInfo.csproj

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,21 @@
1212
<RepositoryUrl>https://github.com/ProgrammerLP/PLP-SystemInfo</RepositoryUrl>
1313
<PackageTags>info, system, system-info, system info, windows</PackageTags>
1414
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
15-
<AssemblyVersion>2.0.0</AssemblyVersion>
16-
<FileVersion>2.0.0</FileVersion>
17-
<Version>2.0.0</Version>
15+
<AssemblyVersion>2.1.0</AssemblyVersion>
16+
<FileVersion>2.1.0</FileVersion>
17+
<Version>2.1.0</Version>
1818
<Copyright>© - ProgrammerLP</Copyright>
19+
<PackageProjectUrl>https://github.com/ProgrammerLP/PLP-SystemInfo</PackageProjectUrl>
20+
<PackageReadmeFile>README.md</PackageReadmeFile>
1921
</PropertyGroup>
2022

23+
<ItemGroup>
24+
<None Include="..\README.md">
25+
<Pack>True</Pack>
26+
<PackagePath>\</PackagePath>
27+
</None>
28+
</ItemGroup>
29+
2130
<ItemGroup>
2231
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
2332
<PackageReference Include="System.Management" Version="8.0.0" />

PLP-SystemInfo/SystemInfo.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,37 @@ namespace PLP_SystemInfo
66
{
77
public static class SystemInfo
88
{
9+
/// <summary>
10+
/// Retruns the username of the current user.
11+
/// </summary>
912
public static string UserName => Environment.UserName;
13+
14+
/// <summary>
15+
/// Retruns the name of the machine.
16+
/// </summary>
1017
public static string MachineName => Environment.MachineName;
11-
public static bool IsDarkModeEnabled => Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", "AppsUseLightTheme", -1) is null;
1218

19+
/// <summary>
20+
/// Returns a boolean value indicating whether Windows Dark Mode is enabled.
21+
/// </summary>
22+
public static bool IsDarkModeEnabled => Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", "AppsUseLightTheme", -1) is null;
1323

24+
/// <summary>
25+
/// Returns the Windows accent color as HEX value.
26+
/// </summary>
27+
/// <returns></returns>
1428
public static string GetWindowsAccentColor()
1529
{
1630
Color color = GetAccentColor();
1731
string c = "#" + color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2");
1832
return c;
1933
}
2034

35+
/// <summary>
36+
/// Returns the Windows accent color as a Color value.
37+
/// </summary>
38+
/// <returns></returns>
39+
/// <exception cref="InvalidOperationException"></exception>
2140
public static Color GetAccentColor()
2241
{
2342
const String DWM_KEY = @"Software\Microsoft\Windows\DWM";

PLP_SystemInfoTest/MainWindow.xaml.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public MainWindow()
4545
list.Items.Add(ProcessorInfo.GetProcessors().ToString());
4646
list.Items.Add(RamInfo.GetRamInfo().ToString());
4747
list.Items.Add(GraphicsInfo.GetGraphicscardInfo().ToString());
48+
49+
list.Items.Add(RamInfo.GetTotalUsableRam());
50+
list.Items.Add(RamInfo.GetAvailableRam());
51+
list.Items.Add(RamInfo.GetRamInUse());
52+
list.Items.Add(RamInfo.GetHardwareReservedRam());
4853
}
4954
}
5055
}

0 commit comments

Comments
 (0)