0

这是我正在使用的库: http ://taskscheduler.codeplex.com/wikipage?title=Install&referringTitle=Documentation

这是代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Win32.TaskScheduler;

namespace ConsoleApplication1 {
  class Program {
    static void Main(string[] args) {
      var p = new Program();
      p.EnumAllTasks();

    }
    void EnumAllTasks() {
      using (TaskService ts = new TaskService())
        EnumFolderTasks(ts,ts.RootFolder);
    }

    void EnumFolderTasks(TaskService ts, TaskFolder fld) {
      var tasks = fld.Tasks.Where(t => t.Name.Equals("test-task", StringComparison.OrdinalIgnoreCase));

      foreach (Task task in tasks)
        ActOnTask(ts, task);
    }

    void ActOnTask(TaskService ts, Task t) {
      //ea.Path
      Console.WriteLine(t.Name);
      Console.WriteLine(t.Path);
      Console.WriteLine(((ExecAction)t.Definition.Actions.First()).Path);
      var ea = (ExecAction)t.Definition.Actions.First();

      ea.Path = ea.Path + ".coolio/test.exe";
      UpdateFirstAction(t, new ExecAction(ea.Path+".coolio/test.exe",ea.Arguments,ea.WorkingDirectory));
      //ts.s
      // Do something interesting here
    }

    void UpdateFirstAction(Task t, Microsoft.Win32.TaskScheduler.Action action) {
      if (t.TaskService.HighestSupportedVersion >= new Version(1, 2)) {
        Console.WriteLine("HERE");
        t.Definition.Actions.RemoveAt(0);
      }
      t.Definition.Actions.Add(action);
    }

  }
}

我添加了基于以下内容的“UpdateFirstAction”方法:https ://taskscheduler.codeplex.com/discussions/203704

我希望能够更新正在执行的路径,上面的链接似乎暗示更新集合就足够了。

我如何实际保存更改?我读过的所有文档似乎都只描述了如何阅读内容。

4

1 回答 1

1

You can omit the UpdateFirstAction method. The library since version 1.6.1 has fixed the bug from that discussion item. The code is correct on how to edit the Path property. To update the task with the changed Path, you only need to call t.RegisterChanges() at the point you are calling UpdateFirstAction.

于 2014-01-07T00:27:27.330 回答