-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
228 lines (204 loc) · 8.38 KB
/
Copy pathProgram.cs
File metadata and controls
228 lines (204 loc) · 8.38 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
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OmniTools
{
internal static class Program
{
// Version locale de l’application
private const string CurrentVersion = "0.8";
// Langue courante
private const string CurrentLanguage = "fr-FR";
// URL où se trouve la dernière version sous forme de texte (par ex. "0.8")
private const string VersionUrl = "https://raw.githubusercontent.com/danbenba/OmniTools/refs/heads/project/version";
// URL pointant vers le nouvel exécutable (fichier .exe) à télécharger
private const string DownloadExeUrl = "https://github.com/danbenba/OmniTools/releases/download/lasted/OmniTools.exe";
// URL de la page release (si vous voulez rediriger l’utilisateur en cas d’erreur ou autre)
private const string ReleaseUrl = "https://github.com/danbenba/OmniTools/releases/latest";
// URL pour la vérification de la connection internet
private const string CheckURL = "https://www.google.com";
// Expose également la version et la langue en public
public const string Version = CurrentVersion;
public const string Language = CurrentLanguage;
//Settings
public static bool OverrideDefenderDisabler { get; set; } = false;
public static bool DetailedLogsEnabled { get; set; } = false;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Lance la SplashForm (ou la fenêtre principale)
Application.Run(new SplashForm());
}
/// <summary>
/// Vérifie s’il existe une nouvelle version en comparant avec un fichier distant.
/// Si l'appel provient de la fenêtre AboutForm (showNotificationWhenUpToDate == true) et qu'il n'y a pas de mise à jour,
/// affiche une notification. Sinon, affiche la popup de mise à jour si une nouvelle version est détectée.
/// </summary>
public static async Task<bool> CheckForUpdates(bool showNotificationWhenUpToDate = false)
{
try
{
using HttpClient client = new HttpClient();
string latestVersion = await client.GetStringAsync(VersionUrl);
latestVersion = latestVersion.Trim();
if (latestVersion == CurrentVersion)
{
// Application déjà à jour
if (showNotificationWhenUpToDate)
{
AboutForm.ShowNotification("Aucune mise à jour n'est disponible.");
}
return true;
}
else
{
// Nouvelle version détectée, on affiche la popup de mise à jour
using (var form = new UpdateForm(latestVersion))
{
var result = form.ShowDialog();
if (result == DialogResult.OK)
{
// L’utilisateur a cliqué sur "Mettre à jour"
string currentExePath = Application.ExecutablePath;
// Ouvrir la fenêtre de progression
using (var frmUpdate = new UpdateDownloadForm(DownloadExeUrl, currentExePath))
{
frmUpdate.ShowDialog();
}
}
}
return true;
}
}
catch (Exception ex)
{
MessageBox.Show(
$"Erreur lors de la vérification des mises à jour : {ex.Message}",
"Erreur",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
return true;
}
}
/// <summary>
/// Vérifie si une URL est joignable en envoyant une requête HEAD.
/// </summary>
public static async Task<bool> IsUrlReachable(string url)
{
try
{
using HttpClient client = new HttpClient();
var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, url));
return response.IsSuccessStatusCode;
}
catch
{
return false;
}
}
/// <summary>
/// Vérifie la connexion Internet et affiche un MessageBox si aucune connexion n’est détectée.
/// </summary>
public static async Task CheckInternetAndNotifyAsync()
{
bool isConnected = await IsInternetConnectionAvailable();
if (!isConnected)
{
MessageBox.Show(
"Aucune connexion Internet détectée. Veuillez vérifier votre connexion.",
"Connexion Internet",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
}
/// <summary>
/// Vérifie s’il y a une connexion internet (ping d’un site fiable).
/// </summary>
public static async Task<bool> IsInternetConnectionAvailable()
{
try
{
using var client = new HttpClient();
using var response = await client.GetAsync(CheckURL);
return response.IsSuccessStatusCode;
}
catch
{
return false;
}
}
/// <summary>
/// Télécharge le nouvel exécutable depuis <paramref name="downloadUrl" />,
/// remplace l’EXE actuel, puis lance la nouvelle version.
/// </summary>
private static async Task DownloadAndReplaceExe(string downloadUrl)
{
// Chemin de l’EXE courant
string currentExePath = Application.ExecutablePath;
string currentFolder = Path.GetDirectoryName(currentExePath);
// On télécharge d’abord le nouveau fichier sous un nom temporaire
string tempExeName = "OmniTools_Update.exe";
string tempExePath = Path.Combine(currentFolder, tempExeName);
try
{
using HttpClient client = new HttpClient();
byte[] newExeBytes = await client.GetByteArrayAsync(downloadUrl);
// On écrit le nouveau fichier à côté de l’EXE actuel
await File.WriteAllBytesAsync(tempExePath, newExeBytes);
}
catch (Exception ex)
{
MessageBox.Show(
$"Erreur lors du téléchargement de la mise à jour : {ex.Message}",
"Erreur",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
return;
}
// Génération d’un script .bat pour remplacer l’EXE actuel
string batFilePath = Path.Combine(Path.GetTempPath(), "OmniTools_Updater.bat");
// Temporisation pour permettre la fermeture de l’application
string batContent = $@"
@echo off
ping 127.0.0.1 -n 2 > nul
del ""{currentExePath}""
move ""{tempExePath}"" ""{currentExePath}""
start """" ""{currentExePath}""
del ""%~f0""
";
File.WriteAllText(batFilePath, batContent);
// On lance le script
try
{
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = batFilePath,
CreateNoWindow = true,
UseShellExecute = false
};
Process.Start(psi);
}
catch (Exception ex)
{
MessageBox.Show(
$"Erreur lors du démarrage de la nouvelle version : {ex.Message}",
"Erreur",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
return;
}
// Ferme l’application en cours
Application.Exit();
}
}
}