-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegistryHelper.cs
74 lines (70 loc) · 2.37 KB
/
RegistryHelper.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
using Microsoft.Win32;
using System;
using System.Net.NetworkInformation;
namespace PlaytimeUpdater
{
public static class RegistryHelper
{
public static int ReadPlaytimeFromRegistry(string registryPath)
{
try
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(registryPath))
{
if (key != null)
{
object value = key.GetValue("CollapseLauncher_Playtime");
if (value is string strValue)
{
return int.Parse(strValue, System.Globalization.NumberStyles.HexNumber);
}
else if (value is int intValue)
{
return intValue;
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error reading registry: {ex.Message}");
}
return 0;
}
public static void SavePlaytimeToRegistry(string registryPath, int value)
{
try
{
using (RegistryKey key = Registry.CurrentUser.CreateSubKey(registryPath))
{
key.SetValue("CollapseLauncher_Playtime", value, RegistryValueKind.DWord);
Console.WriteLine($"Successfully saved playtime to registry: {value}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error saving playtime to registry: {ex.Message}");
}
}
public static string GetRegistryPlaytimeDisplay(string registryPath)
{
int playtime = ReadPlaytimeFromRegistry(registryPath); //in seconds
int hours = playtime / 3600;
int minutes = (playtime % 3600) / 60;
return $"Registry : {hours}h {minutes}m";
}
public static bool CheckRegistryEntry(string path)
{
//check if playtime is saved in registry
RegistryKey Key = Registry.CurrentUser.OpenSubKey(path);
if (Key.GetValue("CollapseLauncher_Playtime") == null)
{
return false;
}
else
{
return true;
}
}
}
}