I have a C# app that will create recurring tasks in the Task Scheduler program to execute a particular program (triggered emails sent based on the recurrence criteria).
Using C# I am able to successfully create the task while running the app in Visual Studio and IIS Express. I see the command window pop up and the task is successfully entered.
However, when entering the program directly through IIS (via a web browser), I see no change in task scheduler and no command window displayed with any status information.
I have tried to run as Administrator to no avail. Based on the code below, what modifications do I need to make to successfully add a scheduled task when one does not exist or modify an existing recurring scheduled task that already exists and just change the days of recurrence?
Here is what I have done so far:
if (chkActivate.SelectedItem.Text == "Deactivate Reminders")
{
chkDaysOfWeek.Enabled = false;
chkEmailOption.Enabled = false;
command = "schtasks.exe /Change /TN \"Action Item Reminder\" /Disable";
}
else
{
chkDaysOfWeek.Enabled = true;
chkEmailOption.Enabled = true;
command = "schtasks.exe /Change /TN \"Action Item Reminder\" /Enable";
}
List<string> days = new List<string>();
for (int idx = 0; idx < chkDaysOfWeek.Items.Count; idx++)
{
if (chkDaysOfWeek.Items[idx].Selected == true)
{
strquery = "UPDATE EmailOption set [Value] = 'true' where [Option] = '" + chkDaysOfWeek.Items[idx].Text.ToString() + "'";
days.Add(chkDaysOfWeek.Items[idx].Text.Substring(0, 3).ToUpper());
}
else
strquery = "UPDATE EmailOption set [Value] = 'false' where [Option] = '" + chkDaysOfWeek.Items[idx].Text.ToString() + "'";
SqlCommand cmd = new SqlCommand(strquery, mycon);
cmd.ExecuteNonQuery();
}
// string runascmd = @"schtasks.exe /DELETE /TN ""Action Item Reminder"" /f & schtasks.exe /CREATE /SC WEEKLY /D " + string.Join(",", days) + @" /TN ""Action Item Reminder"" /TR ""C:\ActionAIM_Source\bin\ActionItemReminder.exe"" /ST 00:01 /f & """ + command;
string runascmd = @"schtasks.exe /CREATE /SC WEEKLY /D " + string.Join(",", days) + @" /TN ""Action Item Reminder"" /TR ""C:\ActionAIM_Source\bin\ActionItemReminder.exe"" /ST 00:01 /f & " + command;
ProcessStartInfo processInfo = new ProcessStartInfo("cmd.exe");
processInfo.Arguments = @"runas /env /user:Administrator /K """ + runascmd + @"""";
//processInfo.Verb = "runas";
processInfo.UseShellExecute = true;
try
{
Process.Start(processInfo);
}
catch (Win32Exception ex)
{
Response.Write(ex.ToString());
}