當前位置: 首頁>>代碼示例>>C#>>正文


C# TaskService.NewTaskFromFile方法代碼示例

本文整理匯總了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);
            }
        }
開發者ID:cgavieta,項目名稱:WORKPAC2016-poc,代碼行數:48,代碼來源:ScheduledTaskManager.cs


注:本文中的Microsoft.Win32.TaskScheduler.TaskService.NewTaskFromFile方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。