I have created a task scheduler and set its trigger time fixed e.g. daily-5:00 pm, but I want to trigger that event when system start or boot. Please help me with code if you have any example.
My tried code:
public static void CreateTask()
{
using (TaskService task = new TaskService())
{
TaskDefinition taskdDef = task.NewTask();
taskdDef.RegistrationInfo.Description = "Does something";
taskdDef.RegistrationInfo.Documentation = "http://www.example.com";
taskdDef.Settings.ExecutionTimeLimit = new TimeSpan(0, 10, 0);
taskdDef.Settings.AllowDemandStart = true;
taskdDef.Actions.Add(new ExecAction(@"D:\Myfolder\bin\SGSclient.exe",
"yourArguments", null));
task.RootFolder.RegisterTaskDefinition("YourTask", taskdDef);
}
}
Using the Task Scheduler Manager Library from GitHub, you could write this
using System;
using Microsoft.Win32.TaskScheduler;
class Program
{
static void Main(string[] args)
{
// Get the service on the local machine
using (TaskService ts = new TaskService())
{
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Does something";
// Create a trigger that will fire after the system boot
td.Triggers.Add(new BootTrigger() );
// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));
// Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition(@"Test", td);
// Remove the task we just created
ts.RootFolder.DeleteTask("Test");
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With