C# 如何禁用任务计划程序中的某些设置字段

C# how to disable certain settings fields in Task Scheduler

我使用 Task Scheduler Managed Wrapper 创建了一个 windows 任务。但是,我不知道如何取消选中设置选项卡中的以下选项:

我当前的代码:

public static void createNewDailyTask(string taskName,string appPath, string description){

using (TaskService ts = new TaskService()) {

    Microsoft.Win32.TaskScheduler.Task t = ts.GetTask(taskName);
    if (t != null) return;

    TaskDefinition td = ts.NewTask();

    td.Principal.RunLevel = TaskRunLevel.Highest;

    td.RegistrationInfo.Description = description;

    TimeTrigger tt = new TimeTrigger();

    // trigger every 5 min
    tt.Repetition.Interval = TimeSpan.FromMinutes(5);

    // Add trigger to the task
    td.Triggers.Add(tt);

    td.Actions.Add(new ExecAction(appPath));

    // Register the task in the root folder
    ts.RootFolder.RegisterTaskDefinition(taskName, td);

    }

}

在 TaskDefinition class 中,有一个设置对象。但是我找不到相应的属性来取消选中前面提到的选项的复选框。

这些复选框似乎可以由属性 TaskDefinition.Settings.ExecutionTimeLimit(类型 String)和 TaskDefintion.Settings.AllowHardTerminate(类型 bool)控制。

ExecutionTimeLimit 被记录为具有不寻常格式的字符串。它说使用 null"PT0S" 无限期地允许 运行:

The amount of time that is allowed to complete the task. The format for this string is PnYnMnDTnHnMnS, where nY is the number of years, nM is the number of months, nD is the number of days, 'T' is the date/time separator, nH is the number of hours, nM is the number of minutes, and nS is the number of seconds (for example, PT5M specifies 5 minutes and P1M4DT2H5M specifies one month, four days, two hours, and five minutes). A value of PT0S will enable the task to run indefinitely. When this parameter is set to Nothing, the execution time limit is infinite.

Source: https://docs.microsoft.com/en-us/windows/desktop/TaskSchd/tasksettings-executiontimelimit

(TaskSettings documentation)

注意:ExecutionTimeLimit 也可能是 TimeSpan。如果是这种情况,请使用 TimeSpan.Zero 作为禁用它的值。