-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
304 lines (286 loc) · 11.6 KB
/
Copy pathProgram.cs
File metadata and controls
304 lines (286 loc) · 11.6 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
namespace ParentalComputerControl
{
class Program
{
static void TimeIntervalControl()
{
try
{
Console.Clear();
HideConsoleWindow();
TimeSpan startHour = Properties.Settings.Default.StartHour;
TimeSpan endHour = Properties.Settings.Default.EndHour;
while (true)
{
TimeSpan currentTime = DateTime.Now.TimeOfDay;
if (!(currentTime >= startHour && currentTime <= endHour))
{
Console.WriteLine("Computer usage is not allowed during the specified hours!");
//Process.Start("shutdown", "/s /t 0");
return;
}
Thread.Sleep(60000);
}
}
catch (Exception ex)
{
ShowConsoleWindow();
Console.WriteLine($"An error occurred: {ex.Message}");
Console.ReadKey();
}
}
static void TotalWorkingTimeControl()
{
try
{
Console.Clear();
HideConsoleWindow();
int totalWorkingHours = Properties.Settings.Default.TotalWorkingHours;
int totalWorkingTime = totalWorkingHours * 60 * 60 * 1000;
int remainingSeconds = Properties.Settings.Default.RemainingSeconds;
DateTime startDate = Properties.Settings.Default.StartDate;
if (startDate.Date == DateTime.Today)
{
if (remainingSeconds <= 0)
{
Console.WriteLine("The designated time is up, computer is shutting down...");
//Process.Start("shutdown", "/s /t 0");
return;
}
}
else
{
remainingSeconds = totalWorkingTime / 1000;
startDate = DateTime.Today;
Properties.Settings.Default.RemainingSeconds = remainingSeconds;
Properties.Settings.Default.StartDate = startDate;
Properties.Settings.Default.Save();
}
DateTime startTime = DateTime.Now;
DateTime endTime = startTime.AddMilliseconds(totalWorkingTime);
while (DateTime.Now < endTime)
{
//Console.WriteLine("Computer is working...");
remainingSeconds = (int)(endTime - DateTime.Now).TotalSeconds;
Properties.Settings.Default.RemainingSeconds = remainingSeconds;
Properties.Settings.Default.Save();
Thread.Sleep(1000);
}
Console.WriteLine("The designated time is up, computer is shutting down...");
//Process.Start("shutdown", "/s /t 0");
}
catch (Exception ex)
{
ShowConsoleWindow();
Console.WriteLine($"An error occurred: {ex.Message}");
Console.ReadKey();
}
}
static void Main(string[] args)
{
InitializeSettings();
RunProgram();
}
static void InitializeSettings()
{
string jsonFilePath = "settings.json";
string password = Properties.Settings.Default.Password;
if (File.Exists(jsonFilePath))
{
string json = File.ReadAllText(jsonFilePath);
Settings settings = JsonConvert.DeserializeObject<Settings>(json);
if (settings.Password == password)
{
UpdateSettings(settings);
settings.Password = "";
string newJson = JsonConvert.SerializeObject(settings, Formatting.Indented);
File.WriteAllText(jsonFilePath, newJson);
}
else
{
Settings oldSettings = new Settings
{
StartHour = Properties.Settings.Default.StartHour.ToString(),
EndHour = Properties.Settings.Default.EndHour.ToString(),
TotalWorkingHours = Properties.Settings.Default.TotalWorkingHours,
Password = ""
};
string oldJson = JsonConvert.SerializeObject(oldSettings, Formatting.Indented);
File.WriteAllText(jsonFilePath, oldJson);
UpdateSettings(settings);
}
}
else
{
var settings = new Settings
{
StartHour = "12:00:00",
EndHour = "16:30:00",
TotalWorkingHours = 5,
Password = ""
};
string newJson = JsonConvert.SerializeObject(settings, Formatting.Indented);
File.WriteAllText(jsonFilePath, newJson);
UpdateSettings(settings);
}
}
static void UpdateSettings(Settings settings)
{
Properties.Settings.Default.StartHour = TimeSpan.Parse(settings.StartHour);
Properties.Settings.Default.EndHour = TimeSpan.Parse(settings.EndHour);
Properties.Settings.Default.TotalWorkingHours = settings.TotalWorkingHours;
Properties.Settings.Default.Save();
}
class Settings
{
public string StartHour { get; set; }
public string EndHour { get; set; }
public int TotalWorkingHours { get; set; }
public string Password { get; set; }
}
static void RunProgram()
{
try
{
ShowConsoleWindow();
Properties.Settings.Default.Reset();
Console.Clear();
string choice = Properties.Settings.Default.UserChoice;
string password = Properties.Settings.Default.Password;
if (string.IsNullOrEmpty(password))
{
Console.Write("Please set a password to proceed: ");
password = Console.ReadLine();
Console.WriteLine("Password set successfully!");
Properties.Settings.Default.Password = password;
Properties.Settings.Default.Save();
}
DateTime startTime = DateTime.Now;
DateTime endTime = startTime.AddSeconds(1);
Console.WriteLine("To change the selection, the password must be entered within 1 seconds.");
Console.Write("Enter password to confirm: ");
StringBuilder inputPasswordBuilder = new StringBuilder();
ConsoleKeyInfo keyInfo;
while (DateTime.Now <= endTime)
{
if (Console.KeyAvailable)
{
keyInfo = Console.ReadKey(true);
if (keyInfo.Key == ConsoleKey.Enter)
break;
if (keyInfo.Key == ConsoleKey.Backspace && inputPasswordBuilder.Length > 0)
{
inputPasswordBuilder.Remove(inputPasswordBuilder.Length - 1, 1);
Console.Write("\b \b");
}
else if (!char.IsControl(keyInfo.KeyChar))
{
inputPasswordBuilder.Append(keyInfo.KeyChar);
Console.Write("*");
}
}
}
string inputPassword = inputPasswordBuilder.ToString();
if (inputPassword == password)
{
Console.Clear();
Console.WriteLine("Select an option:");
Console.WriteLine("1. TotalWorkingTimeControl");
Console.WriteLine("2. TimeIntervalControl");
Console.Write("Enter your choice: ");
choice = Console.ReadLine();
Properties.Settings.Default.UserChoice = choice;
Properties.Settings.Default.Save();
switch (choice)
{
case "1":
TotalWorkingTimeControl();
break;
case "2":
TimeIntervalControl();
break;
default:
Console.WriteLine("Invalid choice. Press any key to try again.");
Console.ReadKey();
break;
}
}
else
{
Console.WriteLine("\nIncorrect password!");
}
if (DateTime.Now >= endTime)
{
Console.WriteLine("Time limit exceeded. Exiting...");
}
switch (choice)
{
case "1":
TotalWorkingTimeControl();
break;
case "2":
TimeIntervalControl();
break;
default:
Console.WriteLine("Select an option:");
Console.WriteLine("1. TotalWorkingTimeControl");
Console.WriteLine("2. TimeIntervalControl");
Console.Write("Enter your choice: ");
choice = Console.ReadLine();
Properties.Settings.Default.UserChoice = choice;
Properties.Settings.Default.Save();
switch (choice)
{
case "1":
TotalWorkingTimeControl();
break;
case "2":
TimeIntervalControl();
break;
default:
Console.WriteLine("Invalid choice. Press any key to try again.");
Console.ReadKey();
break;
}
break;
}
Console.ReadKey();
//HideConsoleWindow(); -- Gizleme
//ShowConsoleWindow(); -- Gösterme
}
catch (Exception ex)
{
ShowConsoleWindow();
Console.WriteLine($"An error occurred: {ex.Message}");
Console.ReadKey();
}
}
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
static void ShowConsoleWindow()
{
IntPtr hWnd = GetConsoleWindow();
if (hWnd != IntPtr.Zero)
{
ShowWindow(hWnd, 1);
}
}
static void HideConsoleWindow()
{
IntPtr hWnd = GetConsoleWindow();
if (hWnd != IntPtr.Zero)
{
ShowWindow(hWnd, 0);
}
}
}
}