Edit scheduled task programmatically

时间:2016-04-21 22:48:38

标签: c# scheduled-tasks pinvoke

I need to be able to edit some settings without user interaction, for example this one:
Screenshot

I already am creating it like that:

using Microsoft.Win32.TaskScheduler;

/* ... */

try
{
    ScheduledTasks.RootFolder.DeleteTask("Task");
}
catch (Exception) { }
TaskDefinition TaskProperties = ScheduledTasks.NewTask();
TaskProperties.RegistrationInfo.Description = "Runs background stuff";
TaskProperties.Actions.Add(new ExecAction(Process.GetCurrentProcess().MainModule.FileName, "/background", null));
ScheduledTasks.RootFolder.RegisterTaskDefinition(@"Task", TaskProperties);

schtasks.exe doesn't give a way to edit it, neither does Task Scheduler Managed Wrapper, but the Task Scheduler GUI does, and there is also a C++ API. How can I edit these settings? I don't really mind if the solution involves PInvoke. Bonus <3 for code snippet.

Note: I do not want a separated XML file to import, since the .exe is supposed to be standalone, and some elements may vary between systems (For example, the location of the file)

1 个答案:

答案 0 :(得分:1)

查看Settings

TaskProperties.Settings.DisallowStartIfOnBatteries = true;