-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
206 lines (182 loc) · 6.19 KB
/
Form1.cs
File metadata and controls
206 lines (182 loc) · 6.19 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
using LibreHardwareMonitor.Hardware;
using System;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Management;
#pragma warning disable CA1050 // Declare types in namespaces
public partial class Form1 : Form
#pragma warning restore CA1050 // Declare types in namespaces
{
private readonly Computer _computer;
private readonly System.Windows.Forms.Timer _timer;
public Form1()
{
InitializeComponent();
_computer = new Computer
{
IsCpuEnabled = true,
IsMotherboardEnabled = true,
IsGpuEnabled = true,
IsMemoryEnabled = true,
IsStorageEnabled = false
};
_computer.Open();
_timer = new System.Windows.Forms.Timer
{
Interval = 1000
};
_timer.Tick += Timer_Tick;
_timer.Start();
}
private void Timer_Tick(object? sender, EventArgs e)
{
UpdateHardwareInfo();
}
private void UpdateHardwareInfo()
{
StringBuilder sb = new StringBuilder();
try
{
string motherboardInfo = GetMotherboardInfo();
string processorInfo = GetProcessorInfo();
string cpuTemperature = GetCpuTemperature();
string cpuUsage = GetCpuUsage();
// string storageInfo = GetStorageInfo();
sb.AppendLine($"Placa Mãe: {motherboardInfo}");
sb.AppendLine($"Processador: {processorInfo}");
sb.AppendLine($"Temperatura da CPU: {cpuTemperature}");
sb.AppendLine($"Uso da CPU: {cpuUsage}");
// sb.AppendLine($"Armazenamento:\n{storageInfo}");
textBoxInfo.Text = sb.ToString();
}
catch (Exception ex)
{
Debug.WriteLine($"Erro ao atualizar informações de hardware: {ex.Message}");
textBoxInfo.Text = "Erro ao obter informações de hardware. Verifique o log para mais detalhes.";
}
}
private string GetMotherboardInfo()
{
foreach (var hardware in _computer.Hardware)
{
if (hardware.HardwareType == HardwareType.Motherboard)
{
return hardware.Name;
}
}
return "Desconhecida";
}
private string GetProcessorInfo()
{
foreach (var hardware in _computer.Hardware)
{
if (hardware.HardwareType == HardwareType.Cpu)
{
return hardware.Name;
}
}
return "Desconhecido";
}
private string GetCpuTemperature()
{
foreach (var hardware in _computer.Hardware)
{
if (hardware.HardwareType == HardwareType.Cpu)
{
foreach (var sensor in hardware.Sensors)
{
if (sensor.SensorType == SensorType.Temperature)
{
return sensor.Value.HasValue ? $"{sensor.Value.Value:F1} °C" : "Desconhecida";
}
}
}
}
return "Desconhecida";
}
private string GetCpuUsage()
{
foreach (var hardware in _computer.Hardware)
{
Console.WriteLine($"Hardware: {hardware.Name}, Type: {hardware.HardwareType}");
if (hardware.HardwareType == HardwareType.Cpu)
{
foreach (var sensor in hardware.Sensors)
{
Console.WriteLine($"Sensor: {sensor.Name}, Type: {sensor.SensorType}, Value: {sensor.Value}");
if (sensor.SensorType == SensorType.Load)
{
return sensor.Value.HasValue ? $"{sensor.Value.Value:F1}%" : "Desconhecido";
}
}
}
}
return "Desconhecido";
}
private string GetStorageInfo()
{
StringBuilder sb = new StringBuilder();
bool storageFound = false;
foreach (var hardware in _computer.Hardware)
{
if (hardware.HardwareType == HardwareType.Storage)
{
storageFound = true;
sb.AppendLine($" - {hardware.Name}");
double? temperature = null;
hardware.Update();
foreach (var sensor in hardware.Sensors)
{
if (sensor.SensorType == SensorType.Temperature)
{
temperature = sensor.Value;
break;
}
}
sb.AppendLine($" Temperatura: {(temperature.HasValue ? $"{temperature.Value:F1} °C" : "Desconhecida")}");
// Tenta obter informações de espaço usando WMI
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk WHERE DriveType=3");
foreach (ManagementObject disk in searcher.Get().Cast<ManagementObject>())
{
string? driveLetter = disk["DeviceID"]?.ToString();
long freeSpace = Convert.ToInt64(disk["FreeSpace"]);
long totalSpace = Convert.ToInt64(disk["Size"]);
long usedSpace = totalSpace - freeSpace;
sb.AppendLine($" Unidade: {driveLetter}");
sb.AppendLine($" Espaço Total: {FormatBytes(totalSpace)}");
sb.AppendLine($" Espaço Usado: {FormatBytes(usedSpace)}");
sb.AppendLine($" Espaço Disponível: {FormatBytes(freeSpace)}");
}
}
catch (Exception ex)
{
sb.AppendLine($" Erro ao obter informações de espaço: {ex.Message}");
}
sb.AppendLine();
}
}
return storageFound ? sb.ToString() : "Nenhum dispositivo de armazenamento encontrado.";
}
private static string FormatBytes(long bytes)
{
string[] suffixes = ["B", "KB", "MB", "GB", "TB", "PB"];
int counter = 0;
decimal number = (decimal)bytes;
while (Math.Round(number / 1024) >= 1)
{
number /= 1024;
counter++;
}
return string.Format("{0:n1} {1}", number, suffixes[counter]);
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
_timer.Stop();
_computer.Close();
base.OnFormClosing(e);
}
}