我有一个C#应用程序,它将在任务计划程序中创建重复任务以执行特定程序(根据重复标准发送的触发电子邮件)。
使用C#我可以在Visual Studio和IIS Express中运行应用程序时成功创建任务。我看到命令窗口弹出,任务成功输入。
但是,当直接通过IIS(通过Web浏览器)进入程序时,我看到任务计划程序没有任何变化,也没有显示任何状态信息的命令窗口。
我试图以管理员身份运行无济于事。根据下面的代码,当一个计划任务不存在时,我需要做哪些修改才能成功添加计划任务,或者修改已经存在的现有重复计划任务,只是改变重复的日期?
这是我到目前为止所做的:
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());
}
答案 0 :(得分:0)
修改代码以使用命令ProcessStartInfo(&#34; schtasks.exe&#34;)而不是ProcessStartInfo(&#34; cmd.exe&#34;)后,我将命令行字符串拆分为两个命令并将App Pool设置为我的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());
}