[英]How do I create/modify a recurring task in C# using Windows Task Scheduler?
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). 我有一个C#应用程序,它将在Task Scheduler程序中创建重复任务以执行特定程序(基于重复条件发送的已触发电子邮件)。
Using C# I am able to successfully create the task while running the app in Visual Studio and IIS Express. 使用C#,我可以在Visual Studio和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. 但是,当直接通过IIS(通过Web浏览器)输入程序时,我看不到任务计划程序中的任何更改,也没有显示带有任何状态信息的命令窗口。
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());
}
After modifying the code to have the command ProcessStartInfo("schtasks.exe") instead of ProcessStartInfo("cmd.exe"), I then split the command line string into two commands and set the App Pool to run as my local account for Windows, I was able to create the task and modify the task I created successfully via IIS. 修改代码以使用命令ProcessStartInfo(“ schtasks.exe”)而不是ProcessStartInfo(“ cmd.exe”)之后,我将命令行字符串拆分为两个命令,并将应用程序池设置为作为Windows的本地帐户运行,我能够创建任务并修改通过IIS成功创建的任务。
if (chkActivate.SelectedItem.Text == "Deactivate Reminders")
{
chkDaysOfWeek.Enabled = false;
chkEmailOption.Enabled = false;
command = @"/Change /TN ""Action Item Reminder"" /Disable";
}
else
{
chkDaysOfWeek.Enabled = true;
chkEmailOption.Enabled = true;
command = @"/Change /TN ""Action Item Reminder"" /Enable";
}
if (chkEmailOption.SelectedItem.Text == "Original")
{
strquery = "UPDATE EmailOption set [Value] = 'true' where [Option] = 'Original Due Date'; UPDATE EmailOption set [Value] = 'false' where [Option] = 'ECD'";
SqlCommand cmd = new SqlCommand(strquery, mycon);
cmd.ExecuteNonQuery();
}
else if (chkEmailOption.SelectedItem.Text == "ECD")
{
strquery = "UPDATE EmailOption set [Value] = 'true' where [Option] = 'ECD'; UPDATE EmailOption set [Value] = 'false' where [Option] = 'Original Due Date'";
SqlCommand cmd = new SqlCommand(strquery, mycon);
cmd.ExecuteNonQuery();
}
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 create = @"/CREATE /SC WEEKLY /D " + string.Join(",", days) + @" /TN ""Action Item Reminder"" /TR ""C:\ActionAIM_Source\bin\ActionItemReminder.exe"" /ST 00:01 /f";
ProcessStartInfo processInfo = new ProcessStartInfo("schtasks.exe");
try
{
processInfo.Arguments = create;
var process = Process.Start(processInfo);
process.WaitForExit(1000);
processInfo.Arguments = command;
process = Process.Start(processInfo);
process.WaitForExit(1000);
}
catch (Win32Exception ex)
{
Response.Write(ex.ToString());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.