在 C# 中系统启动时触发任务调度程序 [英] Task scheduler trigger when system boot in C#
本文介绍了在 C# 中系统启动时触发任务调度程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个任务调度程序并将其触发时间设置为固定的,例如每天下午 5:00,但我想在系统启动或引导时触发该事件.如果您有任何示例,请帮助我编写代码.
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.
提前致谢.
代码:--------------------------------------------------
Code :------------------------------------------------------
public static void CreateTask()
{
using (TaskService task = new TaskService())
{`enter code here`
TaskDefinition taskdDef = task.NewTask();
taskdDef.RegistrationInfo.Description = "Does something";
taskdDef.RegistrationInfo.Documentation = "http://www.mysite.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);
}
}
推荐答案
使用 Task Scheduler Manager Library 从 GitHub,你可以写这个
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");
}
}
}
这篇关于在 C# 中系统启动时触发任务调度程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文