-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
152 lines (129 loc) · 5.61 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
using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32.TaskScheduler;
using Newtonsoft.Json;
namespace OLTAB_Manager
{
public partial class Form1 : Form
{
string configFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data");
string logFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data", "backup_log.txt");
string scriptFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "scripts");
public Form1()
{
InitializeComponent();
InitializeProjectFolders();
InitializeScheduleOptions();
}
private void InitializeProjectFolders()
{
if (!Directory.Exists(configFolderPath))
{
Directory.CreateDirectory(configFolderPath);
}
if (!Directory.Exists(scriptFolderPath))
{
Directory.CreateDirectory(scriptFolderPath);
}
}
private void InitializeScheduleOptions()
{
cboSchedule.Items.AddRange(new string[] { "Daily", "Weekly", "Monthly" });
cboSchedule.SelectedIndex = 0;
}
private void btnBrowse_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog fbd = new FolderBrowserDialog())
{
if (fbd.ShowDialog() == DialogResult.OK)
{
txtFolderPath.Text = fbd.SelectedPath;
}
}
}
private void btnSchedule_Click(object sender, EventArgs e)
{
string folderPath = txtFolderPath.Text;
string scheduleOption = cboSchedule.SelectedItem.ToString();
string time = timePicker.Value.ToString("HH:mm");
if (string.IsNullOrEmpty(folderPath) || !Directory.Exists(folderPath))
{
MessageBox.Show("Please select a valid folder.", "Invalid Folder", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
SaveConfig(folderPath, scheduleOption, time);
try
{
ScheduleBackupTask(folderPath, scheduleOption, time);
MessageBox.Show("Backup Scheduled Successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Failed to schedule backup. Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void SaveConfig(string folderPath, string scheduleOption, string time)
{
var configData = new
{
FolderPath = folderPath,
ScheduleOption = scheduleOption,
Time = time
};
if (!Directory.Exists(configFolderPath))
{
Directory.CreateDirectory(configFolderPath);
}
string configFilePath = Path.Combine(configFolderPath, "config.json");
string configJson = JsonConvert.SerializeObject(configData, Formatting.Indented);
File.WriteAllText(configFilePath, configJson);
}
private void ScheduleBackupTask(string folderPath, string scheduleOption, string time)
{
using (TaskService ts = new TaskService())
{
string taskName = $"OLTAB_Backup_Task_{Path.GetFileName(folderPath)}";
try
{
if (ts.GetTask(taskName) != null)
{
ts.RootFolder.DeleteTask(taskName);
}
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = $"Scheduled Backup Task for {folderPath}";
string[] timeParts = time.Split(':');
int hour = int.Parse(timeParts[0]);
int minute = int.Parse(timeParts[1]);
DateTime startBoundary = DateTime.Today.AddHours(hour).AddMinutes(minute);
if (startBoundary < DateTime.Now)
{
startBoundary = startBoundary.AddDays(1);
}
switch (scheduleOption)
{
case "Daily":
td.Triggers.Add(new DailyTrigger { DaysInterval = 1, StartBoundary = startBoundary });
break;
case "Weekly":
td.Triggers.Add(new WeeklyTrigger { WeeksInterval = 1, StartBoundary = startBoundary });
break;
case "Monthly":
td.Triggers.Add(new MonthlyTrigger { StartBoundary = startBoundary });
break;
}
td.Principal.RunLevel = TaskRunLevel.Highest;
string scriptPath = Path.Combine(scriptFolderPath, "compressor.ps1");
td.Actions.Add(new ExecAction("powershell.exe", $"-ExecutionPolicy Bypass -File \"{scriptPath}\" -FolderPath \"{folderPath}\""));
ts.RootFolder.RegisterTaskDefinition(taskName, td);
File.AppendAllText(logFilePath, $"[{DateTime.Now}] Task scheduled: {taskName} for {scheduleOption} at {time}\n");
}
catch (Exception ex)
{
File.AppendAllText(logFilePath, $"[{DateTime.Now}] Failed to schedule task: {taskName}. Error: {ex.Message}\n");
throw;
}
}
}
}
}