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
|
public void CreateOrUpdateTask(BackupProfile profile)
{
if (!profile.IsScheduleEnabled || !profile.NextScheduledBackup.HasValue)
return;
string folderPath = "BackupManager";
string taskName = $"{folderPath}\\{profile.Name}";
string appPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
string arguments = $"--run-backup \"{profile.Name}\"";
// Créer le dossier s'il n'existe pas
using (TaskService ts = new TaskService())
{
if (!ts.RootFolder.SubFolders.Exists(folderPath))
{
ts.RootFolder.CreateFolder(folderPath);
}
}
// Formater la date et l'heure pour SCHTASKS
DateTime startTime = profile.NextScheduledBackup.Value;
string scheduledTime = startTime.ToString("HH:mm");
string scheduledDate = startTime.ToString("dd/MM/yyyy");
// Construire la commande SCHTASKS selon le type de planification
string scheduleType;
string interval = "";
switch (profile.ScheduleType)
{
case BackupScheduleType.UneFois:
scheduleType = "ONCE";
break;
case BackupScheduleType.Quotidien:
scheduleType = "DAILY";
interval = $"/MO {profile.ScheduleInterval}";
break;
case BackupScheduleType.Hebdomadaire:
scheduleType = "WEEKLY";
string dayOfWeek = startTime.DayOfWeek.ToString().ToUpper().Substring(0, 3);
interval = $"/D {dayOfWeek} /MO {profile.ScheduleInterval}";
break;
case BackupScheduleType.Mensuel:
scheduleType = "MONTHLY";
int day = startTime.Day;
interval = $"/D {day} /MO {profile.ScheduleInterval}";
break;
case BackupScheduleType.Horaire:
scheduleType = "HOURLY";
interval = $"/MO {profile.ScheduleInterval}";
break;
default:
scheduleType = "ONCE";
break;
}
// Créer la commande complète
// /RL HIGHEST : Exécuter avec les privilèges les plus élevés
// /RU SYSTEM : Exécuter en tant que SYSTEM (pas besoin de connexion utilisateur)
string schtasksCmd = $"SCHTASKS /CREATE /F /TN \"{taskName}\" /TR \"\\\"{appPath}\\\" {arguments}\" /SC {scheduleType} {interval} /SD {scheduledDate} /ST {scheduledTime} /RL HIGHEST /RU SYSTEM /IT";
System.Diagnostics.Debug.WriteLine($"Commande SCHTASKS: {schtasksCmd}");
try
{
// Exécuter la commande
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = "schtasks.exe", // Exécution directe de SCHTASKS
Arguments = $"/CREATE /F /TN \"{taskName}\" /TR \"\\\"{appPath}\\\" {arguments}\" /SC {scheduleType} {interval} /SD {scheduledDate} /ST {scheduledTime} /RL HIGHEST /RU SYSTEM /IT",
UseShellExecute = false, // Nécessaire pour rediriger la sortie
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using (Process process = Process.Start(psi))
{
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
System.Diagnostics.Debug.WriteLine($"SCHTASKS Output: {output}");
System.Diagnostics.Debug.WriteLine($"SCHTASKS Error: {error}");
if (process.ExitCode == 0)
{
System.Diagnostics.Debug.WriteLine($"Tâche créée avec succès: {taskName}");
}
else
{
System.Diagnostics.Debug.WriteLine($"Erreur lors de la création de la tâche. Code de sortie: {process.ExitCode}");
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Exception lors de la création de la tâche: {ex.Message}");
}
} |
Partager