-
-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathFormTaskScheduler.cs
More file actions
214 lines (191 loc) · 9.11 KB
/
Copy pathFormTaskScheduler.cs
File metadata and controls
214 lines (191 loc) · 9.11 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Security.Principal;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32.TaskScheduler;
namespace Backup_All_Databases
{
public partial class FormTaskScheduler : Backup_All_Databases.baseform
{
public FormTaskScheduler()
{
InitializeComponent();
try
{
string currentUser = GetUsername();
lbUser.Text = currentUser;
}
catch (Exception ex)
{
MessageBox.Show($"Failed to load current user: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
lbUser.Text = "Error loading user";
}
}
string GetUsername()
{
string uname = WindowsIdentity.GetCurrent().Name;
return uname.Substring(uname.IndexOf("\\") + 1);
}
bool RequestAdminPrivilege()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
{
MessageBox.Show("This program will now restart to run as \"Administrator\".");
try
{
// Prepare to restart the application with elevation
ProcessStartInfo processInfo = new ProcessStartInfo
{
UseShellExecute = true,
FileName = Application.ExecutablePath, // Path to the current executable
Verb = "runas" // Requests elevation via UAC
};
// Start the new elevated process
Process.Start(processInfo);
// Close the current non-elevated instance
Application.Exit();
}
catch (Exception ex)
{
// Handle cases where the user cancels the UAC prompt or another error occurs
MessageBox.Show($"Failed to elevate permissions: {ex.Message}\nPlease run the application as an administrator.", "Elevation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit(); // Exit anyway to avoid running without permissions
}
}
return true;
}
private void btCreateTaskScheduler_Click(object sender, EventArgs e)
{
if (!RequestAdminPrivilege())
{
return;
}
try
{
// Prompt for admin credentials to allow task to run whether user is logged on or not
(string adminUser, string adminPassword) = PromptForUsername();
if (string.IsNullOrEmpty(adminUser))
{
return;
}
if (string.IsNullOrEmpty(adminPassword))
{
MessageBox.Show("Admin username and password are required to run the task whether logged on or not.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Load the config to get the backup time
Config config = Program.ReadConfigFile(false);
if (config == null)
{
MessageBox.Show("No configuration found. Please save settings first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Create or update the task
using (TaskService ts = new TaskService())
{
// Define the task
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Daily backup of all databases using Backup_All_Databases application";
td.Principal.UserId = adminUser;
td.Principal.LogonType = TaskLogonType.Password; // Run whether user is logged on or not
td.Principal.RunLevel = TaskRunLevel.Highest; // Run with highest privileges
td.Settings.DisallowStartIfOnBatteries = false; // Allow running on battery power
td.Settings.ExecutionTimeLimit = TimeSpan.FromDays(1); // Allow task to run for up to 1 day
td.Settings.Enabled = true; // Ensure task is enabled
td.Settings.Hidden = false; // Optional: make task visible in Task Scheduler
// Set daily trigger at the specified time
DailyTrigger dt = new DailyTrigger
{
StartBoundary = DateTime.Today.AddHours(config.StartHour).AddMinutes(config.StartMinute),
DaysInterval = 1 // Run every day
};
td.Triggers.Add(dt);
// Set the action to run the application with /SILENT argument
string exePath = Application.ExecutablePath;
ExecAction action = new ExecAction(exePath, "/SILENT", null); // Corrected argument syntax
td.Actions.Add(action);
// Register the task (create or update) with credentials
ts.RootFolder.RegisterTaskDefinition("Backup All Databases MySQL", td, TaskCreation.CreateOrUpdate, adminUser, adminPassword, TaskLogonType.Password);
MessageBox.Show("Task scheduler created/updated successfully to run whether user is logged on or not.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show($"Failed to create/update task scheduler: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// Placeholder method to prompt for admin username
private (string, string) PromptForUsername()
{
string password = "";
string username = "";
using (baseform prompt = new baseform())
{
prompt.TopMost = true;
prompt.ShowInTaskbar = false;
prompt.MaximizeBox = false;
prompt.MinimizeBox = false;
prompt.Width = 350;
prompt.Height = 240;
prompt.Text = "Enter Admin Username";
Label label = new Label() { Left = 20, Top = 20, Text = "Windows Current Username:", AutoSize = true };
TextBox textBoxUsername = new TextBox() { Left = 20, Top = 40, Width = 240 };
textBoxUsername.Text = GetUsername();
Label labelpwd = new Label() { Left = 20, Top = 80, Text = "Windows User Login Password:", AutoSize = true };
TextBox textBoxPwd = new TextBox() { Left = 20, Top = 100, Width = 240, UseSystemPasswordChar = true };
Button ok = new Button() { Text = "OK", Left = 20, Top = 140, Width = 100, Height = 30, DialogResult = DialogResult.OK };
Button cancel = new Button() { Text = "Cancel", Left = 130, Top = 140, Width = 100, Height = 30, DialogResult = DialogResult.Cancel };
prompt.Controls.Add(label);
prompt.Controls.Add(textBoxUsername);
prompt.Controls.Add(labelpwd);
prompt.Controls.Add(textBoxPwd);
prompt.Controls.Add(ok);
prompt.Controls.Add(cancel);
prompt.AcceptButton = ok;
prompt.CancelButton = cancel;
if (prompt.ShowDialog() == DialogResult.OK)
{
username = textBoxUsername.Text.Trim();
password = textBoxPwd.Text.Trim();
}
}
if (username == "")
return (null, null);
return (username, password);
}
private void btDeleteTaskScheduler_Click(object sender, EventArgs e)
{
if (!RequestAdminPrivilege())
{
return;
}
try
{
using (TaskService ts = new TaskService())
{
// Check if the task exists
Task task = ts.GetTask("BackupAllDatabasesTask");
if (task == null)
{
MessageBox.Show("No task scheduler found to delete.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
// Delete the task
ts.RootFolder.DeleteTask("BackupAllDatabasesTask");
MessageBox.Show("Task scheduler deleted successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show($"Failed to delete task scheduler: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}