我用 c# 编写了一个代码,以编程方式安排所有必需字段(频率、重复、每月或每周等)的任务。使用“用户登录时运行”选项可以正常工作。
但是我需要我的代码来安排“无论用户是否登录都运行”下的任务。请给我一个解决方案。我使用了以下代码。
private void btn_Build_Click(object sender, EventArgs e)
{
// string xmlOutput = txt_DestinationPath.Text + "\\" + txtTaskName.Text + ".xml";
string xmlOutput = "d:\\BuildSchedule.xml";
using (XmlWriter writer = XmlTextWriter.Create(xmlOutput))
{
string frequency = "Once";
string sourceControl="";
int recur = 1 ;
if (txtRecur.Text != string.Empty)
recur = Convert.ToInt32(txtRecur.Text);
string selectedDays="";
string allDays = "mon";
string selectedMonths = "";
string allMonths = "JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC";
if (radio_TFS.Checked == true)
sourceControl = "TFS";
else if (radio_VSS.Checked == true)
sourceControl = "VSS";
writer.WriteStartDocument();
writer.WriteStartElement("ConfigurationSettings");
writer.WriteStartElement("Credentials");
writer.WriteElementString("URI",txtURI.Text.ToString());
writer.WriteElementString("UserName","");
writer.WriteElementString("Password", "");
writer.WriteElementString("SourceControl", sourceControl);
writer.WriteElementString("TaskName", "buildSchedule");
writer.WriteEndElement();
writer.WriteStartElement("Schedule");
if (radioDaily.Checked == true)
frequency = "Daily";
else if (radioWeekly.Checked == true)
{
frequency = "Weekly";
foreach (string row in listDays.CheckedItems)
selectedDays = selectedDays + row + ",";
if (selectedDays != string.Empty)
selectedDays = selectedDays.Remove(selectedDays.Length - 1, 1);
else
selectedDays = allDays;
}
else if (radioMonthly.Checked == true)
{
frequency = "Monthly";
selectedMonths = string.Empty;
foreach (string row in listMonths.CheckedItems)
selectedMonths = selectedMonths + row + ",";
if (selectedMonths != string.Empty)
selectedMonths = selectedMonths.Remove(selectedMonths.Length - 1, 1);
else
selectedMonths = allMonths;
}
writer.WriteElementString("Frequency", frequency);
writer.WriteElementString("Recur", recur.ToString());
writer.WriteElementString("SelectedDays", selectedDays.ToString());
writer.WriteElementString("SelectedMonths", selectedMonths.ToString());
writer.WriteElementString("Date", Convert.ToString(dtpDate.Value.Date.ToString("MM/dd/yyyy")));
writer.WriteElementString("Time", Convert.ToString(dtpTime.Value.ToString("HH:mm:ss")));
writer.WriteEndElement();
writer.WriteStartElement("PathAndFiles");
writer.WriteElementString("ServerPath", txtServerPath.Text);
writer.WriteElementString("SolutionFilePath", txtSolutionPath.Text);
writer.WriteElementString("DestinationFilePath", txt_DestinationPath.Text);
writer.WriteEndElement();
writer.WriteEndElement();
}
schedule1(xmlOutput);
}
void schedule1(string xmlPath)
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlPath);
root = doc.DocumentElement;
string TaskName = root.GetElementsByTagName("TaskName")[0].InnerText;
string frequency = root.GetElementsByTagName("Frequency")[0].InnerText;
string sb = root.GetElementsByTagName("Date")[0].InnerText + " " + root.GetElementsByTagName("Time")[0].InnerText;
// func(TaskName);
using (TaskService ts = new TaskService())
{
string[] path1 = Environment.CurrentDirectory.Split('\\');
int count1 = path1.Length;
count1 = count1 - 3;
string path = "";
for (int index = 0; index < count1; index++)
path = path + path1[index] + "\\";
path = path + "DownloadAndBuild\\bin\\Debug\\DownloadAndBuild.exe";
if(frequency=="Once")
{
////Task t = ts.AddTask(taskName,
//// new TimeTrigger() { StartBoundary = Convert.ToDateTime(sb) + TimeSpan.FromSeconds(2), Enabled = true },
//// //new ExecAction(@"D:\TTS-mapped\BuildAutomation\DownloadAndBuild\bin\Debug\DownloadAndBuild.exe", null, null));
//// new ExecAction(@pathExe, null, null));
Task t = ts.AddTask(TaskName, new TimeTrigger() { StartBoundary = Convert.ToDateTime(sb) + TimeSpan.FromSeconds(2), Enabled = true },
new ExecAction(path, null, null), null, null, TaskLogonType.InteractiveToken);
}
else if (frequency == "Daily")
{
////if (txtRecur.Text != "")
////{
//// Task t = ts.AddTask(taskName,
//// new DailyTrigger() { StartBoundary = Convert.ToDateTime(sb) + TimeSpan.FromSeconds(2), Enabled = true, DaysInterval = Convert.ToInt16(txtRecur.Text) },
//// //new ExecAction(@"D:\TTS-mapped\BuildAutomation\DownloadAndBuild\bin\Debug\DownloadAndBuild.exe",null,null));//C:\Users\rukmanipriya_s\Desktop\TFS_18thJuly\TFS\DownloadAndBuild\bin\Debug
//// new ExecAction(@pathExe, null, null));
////}
////else
////{
//// Task t = ts.AddTask(taskName,
//// new DailyTrigger() { StartBoundary = Convert.ToDateTime(sb) + TimeSpan.FromSeconds(2), Enabled = true },
//// //new ExecAction(@"D:\TTS-mapped\BuildAutomation\DownloadAndBuild\bin\Debug\DownloadAndBuild.exe",null,null));//C:\Users\rukmanipriya_s\Desktop\TFS_18thJuly\TFS\DownloadAndBuild\bin\Debug
//// new ExecAction(@pathExe, null, null));
////}
//if (txtRecur.Text != "")
//{
// Task t = ts.AddTask(taskName, new DailyTrigger(1) { StartBoundary = Convert.ToDateTime(sb) + TimeSpan.FromSeconds(2), Enabled = true, DaysInterval = Convert.ToInt16(txtRecur.Text) }, new ExecAction(path, null, null), null, null, TaskLogonType.InteractiveToken);
//}
//else
//{
// Task t = ts.AddTask(taskName, new DailyTrigger(1) { StartBoundary = Convert.ToDateTime(sb) + TimeSpan.FromSeconds(2), Enabled = true}, new ExecAction(path, null, null), null, null, TaskLogonType.InteractiveToken);
//}
Task t = ts.AddTask(TaskName, new DailyTrigger(1) { StartBoundary = Convert.ToDateTime(sb) + TimeSpan.FromSeconds(2), Enabled = true, DaysInterval = Convert.ToInt16(root.GetElementsByTagName("Recur")[0].InnerText) }, new ExecAction(path, null, null), null, null, TaskLogonType.InteractiveToken);
}
else if (frequency == "Weekly")
{
DaysOfTheWeek dw = DaysOfTheWeek.Sunday - 1;
string[] str = root.GetElementsByTagName("SelectedDays")[0].InnerText.Split(',');
int count = str.Length;
for (int i = 0; i < count; i++)
{
if (str[i] == "mon")
dw = dw | DaysOfTheWeek.Monday;
else if (str[i] == "tue")
dw = dw | DaysOfTheWeek.Tuesday;
else if (str[i] == "wed")
dw = dw | DaysOfTheWeek.Wednesday;
else if (str[i] == "thu")
dw = dw | DaysOfTheWeek.Thursday;
else if (str[i] == "fri")
dw = dw | DaysOfTheWeek.Friday;
else if (str[i] == "sat")
dw = dw | DaysOfTheWeek.Saturday;
else
dw = dw | DaysOfTheWeek.Sunday;
}
// Task t = ts.AddTask(taskName,
//new WeeklyTrigger() { StartBoundary = Convert.ToDateTime(sb) + TimeSpan.FromSeconds(2), Enabled = true, DaysOfWeek = dw, WeeksInterval = Convert.ToInt16(root.GetElementsByTagName("Recur")[0].InnerText) },
////new ExecAction(@"D:\TTS-mapped\BuildAutomation\DownloadAndBuild\bin\Debug\DownloadAndBuild.exe", null, null));
//new ExecAction(@pathExe, null, null));
Task t = ts.AddTask(TaskName, new WeeklyTrigger(DaysOfTheWeek.Sunday, 1) { StartBoundary = Convert.ToDateTime(sb) + TimeSpan.FromSeconds(2), Enabled = true, DaysOfWeek = dw, WeeksInterval = Convert.ToInt16(root.GetElementsByTagName("Recur")[0].InnerText) }, new ExecAction(path, null, null), null, null, TaskLogonType.InteractiveToken);
}
else if(frequency == "Monthly")
{
MonthsOfTheYear dw = MonthsOfTheYear.January - 1;
string[] str = root.GetElementsByTagName("SelectedMonths")[0].InnerText.Split(',');
int count = str.Length;
for (int i = 0; i < count; i++)
{
if (str[i] == "JAN")
dw = dw | MonthsOfTheYear.January;
else if (str[i] == "FEB")
dw = dw | MonthsOfTheYear.February;
else if (str[i] == "MAR")
dw = dw | MonthsOfTheYear.March;
else if (str[i] == "APR")
dw = dw | MonthsOfTheYear.April;
else if (str[i] == "MAY")
dw = dw | MonthsOfTheYear.May;
else if (str[i] == "JUN")
dw = dw | MonthsOfTheYear.June;
else if (str[i] == "JUL")
dw = dw | MonthsOfTheYear.July;
else if (str[i] == "AUG")
dw = dw | MonthsOfTheYear.August;
else if (str[i] == "SEP")
dw = dw | MonthsOfTheYear.September;
else if (str[i] == "OCT")
dw = dw | MonthsOfTheYear.October;
else if (str[i] == "NOV")
dw = dw | MonthsOfTheYear.November;
else
dw = dw | MonthsOfTheYear.December;
}
//Task t = ts.AddTask(taskName,
// new MonthlyTrigger { StartBoundary = Convert.ToDateTime(sb) + TimeSpan.FromSeconds(2), Enabled = true, MonthsOfYear = dw },
// //new ExecAction(@"D:\TTS-mapped\BuildAutomation\DownloadAndBuild\bin\Debug\DownloadAndBuild.exe", null, null));
// new ExecAction(@pathExe, null, null));
Task t = ts.AddTask(TaskName, new MonthlyTrigger(1, MonthsOfTheYear.AllMonths) { StartBoundary = Convert.ToDateTime(sb) + TimeSpan.FromSeconds(2), Enabled = true, MonthsOfYear = dw }, new ExecAction(path, null, null), null, null, TaskLogonType.InteractiveToken);
}
}
}
void schedule(string xmlPath)
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlPath);
root = doc.DocumentElement;
string frequency;
string command;
string date;
int recur;
string time;
string selectedDays;
string selectedMonths;
string taskName;
time = root.GetElementsByTagName("Time")[0].InnerText;
date = root.GetElementsByTagName("Date")[0].InnerText;
frequency = root.GetElementsByTagName("Frequency")[0].InnerText;
recur = Convert.ToInt32(root.GetElementsByTagName("Recur")[0].InnerText);
selectedDays = root.GetElementsByTagName("SelectedDays")[0].InnerText;
selectedMonths = root.GetElementsByTagName("SelectedMonths")[0].InnerText;
taskName = root.GetElementsByTagName("TaskName")[0].InnerText;
command = "SCHTASKS /Create /SC once /TN varun12 /ST 14:06:00 /SD 08/01/2012 /TR DownloadAndBuild.exe";
int exit = FetchProjects.ExecuteCommand(command, 111110);
if(frequency == "once")
command = "SCHTASKS /Create /SC " + frequency + " /TN "+taskName+" /ST " + time + " /SD " + date+ " /TR D:\\demoBuild.bat";
else if (frequency == "weekly" && selectedDays != string.Empty)
command = "SCHTASKS /Create /SC " + frequency + " /MO " + recur + " /D " + selectedDays + " /TN " + taskName + " /ST " + time + " /SD " + date + " /TR D:\\demoBuild.bat";
else if (frequency == "weekly" && selectedDays == string.Empty)
command = "SCHTASKS /Create /SC " + frequency + " /MO " + recur + " /D *" + " /TN " + taskName + " /ST " + time + " /SD " + date + " /TR D:\\demoBuild.bat";
else if(frequency == "monthly" && selectedMonths != string.Empty)
command = "SCHTASKS /Create /SC " + frequency + " /M " + selectedMonths + " /TN " + taskName + " /ST " + time + " /SD " + date + " /TR D:\\demoBuild.bat";
else if (frequency == "monthly" && selectedMonths == string.Empty)
command = "SCHTASKS /Create /SC " + frequency + " /M *" + " /TN " + taskName + " /ST " + time + " /SD " + date + " /TR D:\\demoBuild.bat";
else
command = "SCHTASKS /Create /SC " + frequency + " /MO " + recur + " /TN " + taskName + " /ST " + time + " /SD " + date + " /TR D:\\demoBuild.bat";
// command = "SCHTASKS /Create /SC once /TN varun /ST 18:47:00 /SD 07/26/2012 /TR D:\\demoBuild.bat";
}