本文整理汇总了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);
}
}