Repository navigation
Expand file tree
/
Copy pathAppManager.cs
More file actions
104 lines (89 loc) · 3.49 KB
/
Copy pathAppManager.cs
File metadata and controls
104 lines (89 loc) · 3.49 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 Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
namespace PT2
{
public static class AppManager
{
public static readonly string AppDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "PlayTimer");
public static readonly string PasswordFile = Path.Combine(AppDataFolder, "PT.log");
public static readonly string GameListFile = Path.Combine(AppDataFolder, "PT-list.txt");
public static readonly string SettingsFile = Path.Combine(AppDataFolder, "PT-settings.txt");
public static void EnsureDirectories()
{
if (!Directory.Exists(AppDataFolder))
{
Directory.CreateDirectory(AppDataFolder);
}
DirectoryInfo dirInfo = new DirectoryInfo(AppDataFolder);
if ((dirInfo.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
{
dirInfo.Attributes |= FileAttributes.Hidden;
}
}
public static string HashPassword(string password)
{
using (SHA256 sha256Hash = SHA256.Create())
{
byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(password));
StringBuilder builder = new StringBuilder();
foreach (byte t in bytes)
{
builder.Append(t.ToString("x2"));
}
return builder.ToString();
}
}
public static bool IsPasswordSet() => File.Exists(PasswordFile);
public static bool VerifyPassword(string inputPassword)
{
if (!IsPasswordSet()) return false;
string savedHash = File.ReadAllText(PasswordFile);
return HashPassword(inputPassword) == savedHash;
}
public static void SetPassword(string newPassword)
{
EnsureDirectories();
File.WriteAllText(PasswordFile, HashPassword(newPassword));
}
public static void SetAutostart(bool enable)
{
string taskName = "PlayTimer";
if (enable)
{
string exePath = Application.ExecutablePath;
string createArguments = $"/Create /TN \"{taskName}\" /TR \"\\\"{exePath}\\\" /sillent\" /SC ONLOGON /RL HIGHEST /F";
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "schtasks.exe",
Arguments = createArguments,
CreateNoWindow = true,
UseShellExecute = false
};
using (Process process = Process.Start(psi))
{
process?.WaitForExit();
}
}
else
{
string deleteArguments = $"/Delete /TN \"{taskName}\" /F";
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "schtasks.exe",
Arguments = deleteArguments,
CreateNoWindow = true,
UseShellExecute = false
};
using (Process process = Process.Start(psi))
{
process?.WaitForExit();
}
}
}
}
}