-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
358 lines (291 loc) · 15.2 KB
/
Form1.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
using System;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Configuration;
using System.Drawing;
namespace vinfo
{
public partial class mainForm : Form
{
double totalSYSMemory;
// Performance Counters for monitoring CPU, RAM, and system uptime
PerformanceCounter perfCPU = new PerformanceCounter("Processor", "% Processor Time", "_Total");
PerformanceCounter perfRAM = new PerformanceCounter("Memory", "Available MBytes");
PerformanceCounter perfSYS = new PerformanceCounter("System", "System Up Time");
PerformanceCounter perfDISK = new PerformanceCounter("PhysicalDisk", "% Disk Time","_Total");
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
static extern int SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags);
[Flags]
enum RecycleFlags : uint
{
SHERB_NOCONFIRMATION = 0x00000001,
SHERB_NOPROGRESSUI = 0x00000001,
SHERB_NOSOUND = 0x00000004
}
public mainForm()
{
InitializeComponent();
InitializeTheme("theme");
}
private void InitializeTheme(string themeName)
{
try
{
var theme = ConfigurationManager.AppSettings[themeName];
if(theme == "Light")
{
/* Background form */
this.BackColor = Color.FromArgb(255, 255, 255);
/* Buttons */
btn_cleanup_storage.ForeColor = Color.FromArgb(82, 147, 101);
btn_toggle_theme.ForeColor = Color.FromArgb(46, 52, 56);
/* List */
list_storage.BackColor = Color.FromArgb(255,255,255);
list_storage.ForeColor = Color.FromArgb(46, 52, 56);
/* Progress */
progress_bar_cpu_usage.OuterColor = Color.FromArgb(199, 204, 209);
progress_bar_cpu_usage.ProgressColor = Color.FromArgb(207, 115, 229);
progress_bar_available_usage.OuterColor = Color.FromArgb(199, 204, 209);
progress_bar_available_usage.ProgressColor = Color.FromArgb(72, 208, 166);
progress_bar_disk_usage.OuterColor = Color.FromArgb(199, 204, 209);
progress_bar_disk_usage.ProgressColor = Color.FromArgb(240, 130, 129);
/* Title PC Name */
lbl_pc_name.ForeColor = Color.FromArgb(0, 0, 0);
/* Titles */
lbl_title_performance.ForeColor = Color.FromArgb(46, 52, 56);
lbl_title_hardware.ForeColor = Color.FromArgb(46, 52, 56);
lbl_title_storage.ForeColor = Color.FromArgb(46, 52, 56);
/* Subtitles */
lbl_subtitle_performance.ForeColor = Color.FromArgb(92, 104, 112);
lbl_subtitle_hardware.ForeColor = Color.FromArgb(92, 104, 112);
lbl_subtitle_storage.ForeColor = Color.FromArgb(92, 104, 112);
/* Items */
lbl_item_uptime_system.ForeColor = Color.FromArgb(69, 78, 84);
lbl_cpu_usage.ForeColor = Color.FromArgb(69, 78, 84);
lbl_available_memory.ForeColor = Color.FromArgb(69, 78, 84);
lbl_disk_usage.ForeColor = Color.FromArgb(69, 78, 84);
lbl_item_physical_processors.ForeColor = Color.FromArgb(69, 78, 84);
lbl_item_logical_processors.ForeColor = Color.FromArgb(69, 78, 84);
lbl_item_memory.ForeColor = Color.FromArgb(69, 78, 84);
/* Value Items */
lbl_up_time_system.ForeColor = Color.FromArgb(46, 52, 56);
lbl_physical_processors.ForeColor = Color.FromArgb(46, 52, 56);
lbl_logical_processors.ForeColor = Color.FromArgb(46, 52, 56);
lbl_total_memory.ForeColor = Color.FromArgb(46, 52, 56);
progress_bar_cpu_usage.ForeColor = Color.FromArgb(46, 52, 56);
progress_bar_available_usage.ForeColor = Color.FromArgb(46, 52, 56);
progress_bar_disk_usage.ForeColor = Color.FromArgb(46, 52, 56);
btn_toggle_theme.Text = "Dark Mode";
ConfigurationManager.AppSettings[themeName] = "Dark";
}
else
{
/* Background form */
this.BackColor = Color.FromArgb(0,0,0);
/* Buttons */
btn_cleanup_storage.ForeColor = Color.FromArgb(255, 255, 255);
btn_toggle_theme.ForeColor = Color.FromArgb(255, 255, 255);
/* List */
list_storage.BackColor = Color.FromArgb(0,0,0);
list_storage.ForeColor = Color.FromArgb(255,255,255);
/* Progress */
progress_bar_cpu_usage.OuterColor = Color.FromArgb(52, 57, 62);
progress_bar_cpu_usage.ProgressColor = Color.FromArgb(149, 55, 171);
progress_bar_available_usage.OuterColor = Color.FromArgb(52, 57, 62);
progress_bar_available_usage.ProgressColor = Color.FromArgb(33, 140, 102);
progress_bar_disk_usage.OuterColor = Color.FromArgb(52, 57, 62);
progress_bar_disk_usage.ProgressColor = Color.FromArgb(207, 48, 55);
/* Title PC Name */
lbl_pc_name.ForeColor = Color.FromArgb(255, 255, 255);
/* Titles */
lbl_title_performance.ForeColor = Color.FromArgb(255,255,255);
lbl_title_hardware.ForeColor = Color.FromArgb(255, 255, 255);
lbl_title_storage.ForeColor = Color.FromArgb(255, 255, 255);
/* Subtitles */
lbl_subtitle_performance.ForeColor = Color.FromArgb(128,128,128);
lbl_subtitle_hardware.ForeColor = Color.FromArgb(128, 128, 128);
lbl_subtitle_storage.ForeColor = Color.FromArgb(128, 128, 128);
/* Items */
lbl_item_uptime_system.ForeColor = Color.FromArgb(169, 169, 169);
lbl_cpu_usage.ForeColor = Color.FromArgb(169, 169, 169);
lbl_available_memory.ForeColor = Color.FromArgb(169, 169, 169);
lbl_disk_usage.ForeColor = Color.FromArgb(169, 169, 169);
lbl_item_physical_processors.ForeColor = Color.FromArgb(169, 169, 169);
lbl_item_logical_processors.ForeColor = Color.FromArgb(169, 169, 169);
lbl_item_memory.ForeColor = Color.FromArgb(169, 169, 169);
/* Value Items */
lbl_up_time_system.ForeColor = Color.FromArgb(255, 255, 255);
lbl_physical_processors.ForeColor = Color.FromArgb(255, 255, 255);
lbl_logical_processors.ForeColor = Color.FromArgb(255, 255, 255);
lbl_total_memory.ForeColor = Color.FromArgb(255, 255, 255);
progress_bar_cpu_usage.ForeColor = Color.FromArgb(255, 255, 255);
progress_bar_available_usage.ForeColor = Color.FromArgb(255, 255, 255);
progress_bar_disk_usage.ForeColor = Color.FromArgb(255, 255, 255);
btn_toggle_theme.Text = "Light Mode";
ConfigurationManager.AppSettings[themeName] = "Light";
}
}
catch(Exception ex)
{
throw;
}
}
private void mainForm_Load(object sender, EventArgs e)
{
// Enable timers for updating CPU and memory usage
timer_update_memory_usage.Enabled = true;
timer_update_cpu_usage.Enabled = true;
timer_update_disk_usage.Enabled = true;
timer_update_uptime_system.Enabled = true;
// Retrieve and display system information
GetSystemInfo();
GetStorageInfo();
}
private void GetSystemInfo()
{
try
{
// WMI query to retrieve system information
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem");
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject info in collection)
{
// Total physical processors
int physicalProcessors = Convert.ToInt32(info["NumberOfProcessors"]);
// Total logical processors
int logicalProcessors = Convert.ToInt32(info["NumberOfLogicalProcessors"]);
// Total RAM in bytes
ulong totalMemoryBytes = Convert.ToUInt64(info["TotalPhysicalMemory"]);
//Computer name
lbl_pc_name.Text = Convert.ToString(info["Name"]);
// Total Memory in mb
totalSYSMemory = totalMemoryBytes / (1024.0 * 1024.0);
// Convert bytes to gigabytes for easier reading
double totalMemoryGB = totalMemoryBytes / (1024.0 * 1024.0 * 1024.0);
// Display information
lbl_physical_processors.Text = physicalProcessors.ToString();
lbl_logical_processors.Text = logicalProcessors.ToString();
lbl_total_memory.Text = $"{totalMemoryGB:F2} GB";
}
}
catch (Exception ex)
{
MessageBox.Show("Error obtaining system information: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void timer_update_memory_usage_Tick(object sender, EventArgs e)
{
// Calculate and display available memory usage
progress_bar_available_usage.Text = ((int)((perfRAM.NextValue() / totalSYSMemory) * 100)).ToString() + "%";
// Set progress bar properties for memory usage
progress_bar_available_usage.Minimum = 0;
progress_bar_available_usage.Maximum = (int)totalSYSMemory;
progress_bar_available_usage.Value = (int)perfRAM.NextValue();
}
private void timer_update_cpu_usage_Tick(object sender, EventArgs e)
{
// Calculate and display CPU usage
float cpuUsage = perfCPU.NextValue();
progress_bar_cpu_usage.Text = $"{(int)cpuUsage}%";
// Set progress bar properties for CPU usage
progress_bar_cpu_usage.Minimum = 0;
progress_bar_cpu_usage.Maximum = 100;
progress_bar_cpu_usage.Value = (int)cpuUsage;
}
private void GetStorageInfo()
{
try
{
list_storage.Items.Clear();
// WMI query to retrieve storage information
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject info in collection)
{
// Device ID (Drive letter)
string deviceID = info["DeviceID"].ToString();
// Volume Name
string volumeName = info["VolumeName"] != null ? info["VolumeName"].ToString() : "N/A";
// File System (NTFS, FAT32, etc.)
string fileSystem = info["FileSystem"].ToString();
// Total size in bytes
ulong totalSizeBytes = Convert.ToUInt64(info["Size"]);
// Free space in bytes
ulong freeSpaceBytes = Convert.ToUInt64(info["FreeSpace"]);
// Convert bytes to gigabytes for easier reading
double totalSizeGB = totalSizeBytes / (1024.0 * 1024.0 * 1024.0);
double freeSpaceGB = freeSpaceBytes / (1024.0 * 1024.0 * 1024.0);
double usedSpaceGB = totalSizeGB - freeSpaceGB;
list_storage.Items.Add($"{deviceID} - Used Space: {usedSpaceGB:F2} GB - Free Space: {freeSpaceGB:F2} GB");
}
}
catch (Exception ex)
{
MessageBox.Show("Error obtaining storage information: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void timer_update_disk_usage_Tick(object sender, EventArgs e)
{
// Calculate and display disk usage
float diskUsage = perfDISK.NextValue();
progress_bar_disk_usage.Text = $"{(int)diskUsage}%";
// Set progress bar properties for disk usage
progress_bar_disk_usage.Minimum = 0;
progress_bar_disk_usage.Maximum = 100;
progress_bar_disk_usage.Value = (int)diskUsage < 100 ? (int)diskUsage : 100;
}
private void timer_update_uptime_system_Tick(object sender, EventArgs e)
{
// Display system uptime in minutes
lbl_up_time_system.Text = ((int)perfSYS.NextValue() / 60).ToString() + " minutes";
}
private void btn_cleanup_storage_Click(object sender, EventArgs e)
{
CleanTempDirectory();
EmptyRecycleBin();
MessageBox.Show("Cleanup completed successfully!");
}
static void CleanTempDirectory()
{
string tempPath = Path.GetTempPath();
DirectoryInfo tempDir = new DirectoryInfo(tempPath);
foreach (FileInfo file in tempDir.GetFiles())
{
try
{
file.Delete();
}
catch (Exception ex)
{
Console.WriteLine($"Error deleting file {file.FullName}: {ex.Message}");
}
}
}
static void EmptyRecycleBin()
{
try
{
int result = SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlags.SHERB_NOCONFIRMATION);
if (result == 0)
{
Console.WriteLine("Recycle Bin emptied successfully!");
}
else
{
Console.WriteLine($"Error emptying Recycle Bin. Error code: {result}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error emptying Recycle Bin: {ex.Message}");
}
}
private void btn_toggle_theme_Click(object sender, EventArgs e)
{
InitializeTheme("theme");
}
}
}