本文整理匯總了C#中Microsoft.Win32.TaskScheduler.TaskService.NewTaskFromFile方法的典型用法代碼示例。如果您正苦於以下問題:C# TaskService.NewTaskFromFile方法的具體用法?C# TaskService.NewTaskFromFile怎麽用?C# TaskService.NewTaskFromFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Microsoft.Win32.TaskScheduler.TaskService
的用法示例。
在下文中一共展示了TaskService.NewTaskFromFile方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: InstallScheduledTasks
/// <summary>
/// Installs the windows scheduled tasks.
/// </summary>
public void InstallScheduledTasks()
{
using (TaskService taskService = new TaskService())
{
TaskFolder taskFolder = taskService.RootFolder.SubFolders.FirstOrDefault(f => f.Name == FOLDER_NAME) ?? taskService.RootFolder.CreateFolder(FOLDER_NAME);
string taskXml = AssemblyResourceReader.ReadAsString(TEMPLATE_FILE);
Dictionary<string, string> namespaces = new Dictionary<string, string>
{
{ Constants.Task_XmlNamespacePrefix, Constants.Task_XmlNamespaceUri }
};
List<XmlReplacement> replacements = new List<XmlReplacement>();
string startBoundaryVal = DateTime.Now.ToString("s");
replacements.Create(Constants.XPath_StartBoundary, null, currentValue => startBoundaryVal);
XmlReplacement commandReplacement = replacements.Create(Constants.XPath_Command, null, null);
string json = AssemblyResourceReader.ReadAsString(TASK_JSON_FILE)
.Replace("{websiteName}", HttpUtility.JavaScriptStringEncode(this.installOptions.WebsiteName));
XmlUpdater updater = new XmlUpdater();
JArray tasks = JArray.Parse(json);
foreach (JToken task in tasks)
{
string taskName = task.Value<string>(TASK_NAME_JSON_KEY);
this.installLogger.Log(string.Format(Messages.TASK_CreateScheduledTask, taskName));
XmlDocument doc = new XmlDocument();
doc.LoadXml(taskXml);
string executablePath = Path.Combine(this.installVariables.BasePath, task.Value<string>(COMMAND_PATH_JSON_KEY));
commandReplacement.ReplacementValue = currentValue => executablePath;
updater.Update(doc, namespaces, replacements);
doc.Save(this.tempFile);
TaskDefinition taskDefinition = taskService.NewTaskFromFile(this.tempFile);
float interval = task.Value<float>(INTERVAL_MINUTES_JSON_KEY);
this.AddSchedule(taskDefinition, interval);
taskFolder.RegisterTaskDefinition(taskName, taskDefinition, TaskCreation.CreateOrUpdate, @"NT AUTHORITY\SYSTEM", null, TaskLogonType.ServiceAccount);
this.AddFolderPermission(Directory.GetParent(executablePath).FullName);
this.installLogger.LogSuccess(Messages.MAIN_StepComplete);
}
File.Delete(this.tempFile);
}
}