本文介绍了创建计划任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前工作的一个C#WPF项目。我需要让用户创建并添加计划任务到Windows任务计划程序。
I am currently working on a C# WPF project. I need to allow the user to create and add a scheduled task to the Windows Task Scheduler.
我怎么能去这样做,什么using指令和引用,我需要像搜索互联网的时候,我没有找到太多。
How could I go about doing this and what using directives and references do I need as I am not finding much when searching the Internet.
推荐答案
您可以使用:
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 the task at this time every other day
td.Triggers.Add(new DailyTrigger { DaysInterval = 2 });
// 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");
}
}
}
另外,您可以使用 API或去。请参见了解详细信息。
Alternatively you can use native API or go for Quartz.NET. See this for details.
这篇关于创建计划任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!