dahall/TaskScheduler
GitHub: dahall/TaskScheduler
为 Windows 任务计划程序提供统一的 .NET 托管包装器,支持多版本 COM 接口聚合、GUI 编辑器和多语言本地化。
Stars: 1347 | Forks: 201
#  Task Scheduler 托管包装器
[](https://github.com/dahall/TaskScheduler/releases) [](https://www.nuget.org/packages/TaskScheduler/) [](https://ci.appveyor.com/project/dahall/taskscheduler)
## 快速链接
* [Wiki](https://github.com/dahall/TaskScheduler/wiki) - 示例代码、库使用指南、故障排除等。
* [API 文档](https://dahall.github.io/TaskScheduler) - 类/方法/属性文档及示例
* [完整问题日志](https://github.com/dahall/TaskScheduler/issues?q=) - 使用搜索框查看您的问题是否已有解答。
* [讨论论坛](https://github.com/dahall/TaskScheduler/discussions) - 用户互助、功能增强请求、问答(已弃用的 Google 论坛在[这里](https://groups.google.com/forum/#!forum/taskscheduler))
* [ITaskHandler 模板和示例项目](https://github.com/dahall/ITaskHandlerTemplate) - 使用它来创建您自己的基于 COM 的程序集,用于进程内操作。
* [故障排除工具](https://github.com/dahall/TaskSchedulerConfig) - 帮助识别和修复配置及连接问题的工具。(ClickOnce 安装程序在[这里](https://github.com/dahall/TaskSchedulerConfig/blob/master/publish/setup.exe?raw=true))
## 安装
该项目的程序集可以通过 NuGet 获取,或者从 [Releases](https://github.com/dahall/TaskScheduler/releases) 部分的 .zip 文件中手动下载。
|链接|包名称|描述|
|------------|------------|-----------|
|[](https://www.nuget.org/packages/TaskScheduler)| TaskScheduler|主库|
|[](https://www.nuget.org/packages/TaskSchedulerEditor)|TaskSchedulerEditor|UI 库|
您还可以在[项目的 AppVeyor NuGet 源](https://ci.appveyor.com/nuget/taskscheduler-prerelease)中找到预发布 NuGet 包。
一旦被您的项目引用,所有的类都可以在 `Microsoft.Win32.TaskScheduler` 命名空间下找到。
## 项目组件
### 主库
微软在 Windows Vista 中引入了具有全新对象模型的 2.0 版(内部版本为 1.2)。这个托管程序集与新对象模型非常相似,但允许操作 1.0(内部版本为 1.1)COM 对象。它会自动选择主机系统上可用的最新版本的库(最高支持到 1.4)。核心功能包括:
* 为 .NET 2.0、3.5、4.0、4.52、5.0、6.0、.NET Standard 2.0、.NET Core 2.0、2.1、3.0、3.1 提供独立且功能一致的库。
* 与基础的 COM 库不同,此包装器有助于创建和查看上下游的任务。
* 使用 C# 编写,但兼容任何 .NET 语言,包括脚本语言(例如 PowerShell)。
* 支持所有 V2 原生属性,甚至在 V1 系统上也是如此。
* 对于 Win8 之后已弃用这些操作的系统,使用 PowerShell 脚本维护 EmailAction 和 ShowMessageAction。
* 在 V1 系统(XP/WS2003)及更早版本(如果已安装 PowerShell)上支持所有操作类型(不仅仅是 ExecAction)。
* 在 V1 系统(XP/WS2003)上支持多重操作。原生库仅支持单一操作。
* 支持将 1.0 和 2.0 任务序列化为 XML(基础库仅支持 2.0)
* 支持针对目标版本的任务验证。
* 支持安全的任务读取和维护。
* 提供用于创建任务的 Fluent 方法。
* 提供用于创建触发器的 Cron 语法。
* 支持 Win8 及更高版本下读取“自定义”触发器。
* 包含大量变通方法和检查,以弥补基础库的不足。
目前支持的本地化语言包括:英语、西班牙语、意大利语、法语、简体中文、德语、波兰语和俄语。如有需要,可应要求添加其他本地化语言,特别是如果您愿意协助翻译的话。
该项目基于原作者于 2002 年 1 月开始使用 1.0 库进行的工作,该库目前托管在 [CodeProject](http://www.codeproject.com/KB/system/taskschedulerlibrary.aspx) 上。
### UI 库
第二个库包含本地化和可本地化的 GUI 编辑器以及任务向导,它们模仿了 Vista 及更高版本中的系统编辑器,并为新属性添加了可选页面。以下是可用的 UI 控件列表:
* 模仿系统编辑器的任务编辑器对话框和选项卡控件 (TaskEditDialog)
* 使用更新 UI 方案的任务编辑器对话框 (TaskOptionsEditor)
* 模仿系统基础编辑器的任务创建向导
* 操作编辑器对话框
* 触发器编辑器对话框
* 事件查看器对话框
* 任务/任务文件夹选择对话框
* 任务历史记录查看器
* 任务运行时间查看器
* 任务服务连接对话框
* 用于从系统事件日志中提取事件的简化类。
* 一个 DropDownCheckList 控件,对于选择标志类型枚举非常有用。
* 一个 FullDateTimePicker 控件,允许在单个控件中同时选择日期和时间。
* 一个 CredentialsDialog 类,用于提示输入密码,该类封装了 Windows API。
## 示例代码
下载文件中包含一个帮助文档,提供了各类的概述。在“Documentation”标签下有大量示例。
您只需一行代码即可执行多项操作:
```
// Run a program every day on the local machine
TaskService.Instance.AddTask("Test", QuickTriggerType.Daily, "myprogram.exe", "-a arg");
// Run a custom COM handler on the last day of every month
TaskService.Instance.AddTask("Test", new MonthlyTrigger { RunOnLastDayOfMonth = true },
new ComHandlerAction(new Guid("{CE7D4428-8A77-4c5d-8A13-5CAB5D1EC734}")));
```
如需更多选项,请使用库类来构建复杂的任务。下面是一个关于如何在 C# 中使用该库的简单示例。
```
using System;
using Microsoft.Win32.TaskScheduler;
class Program
{
static void Main(string[] args)
{
// Get the service on the remote machine
using (TaskService ts = new TaskService(@"\\RemoteServer", "username", "domain", "password"))
{
// 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.
// (Use the username here to ensure remote registration works.)
ts.RootFolder.RegisterTaskDefinition(@"Test", td, TaskCreation.CreateOrUpdate, "username");
}
}
}
```
有关如何使用该库的更多扩展示例,请查看源代码区域或访问[示例页面](https://github.com/dahall/TaskScheduler/wiki/Examples)。该库严格遵循 Task Scheduler 2.0 脚本类。微软在 [Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-start-page) 上提供了一些相关示例,可能会进一步帮助您了解如何使用此库。
本项目鸣谢使用:
[
来自 JetBrains 的 ReSharper](https://www.jetbrains.com/?from=Task%20Scheduler%20Managed%20Wrapper)
[
Sandcastle Help File Builder](https://github.com/EWSoftware/SHFB)
Sandcastle Help File Builder](https://github.com/EWSoftware/SHFB)标签:SOC Prime, 任务调度, 多人体追踪, 开发工具, 系统API封装