这需要一些整理,所以这里有完整的文档来安排来自安装项目的任务。
  
  
   一旦创建了部署项目,就需要使用
   
    Custom Actions
   
   安排任务。
   
    Walkthrough: Creating a Custom Action
   
  
  
   
    注:
   
   演练要求您将主输出添加到安装节点,即使您不打算在安装步骤中执行任何自定义操作。
   
    这很重要,所以不要像我那样忽视它。
   
   安装程序类在此步骤中执行一些状态管理,需要运行。
  
  
   下一步是将安装目录传递给自定义操作。这是通过
   
    CustomActionData property
   
   . 我进入
   
    /DIR="[TARGETDIR]\"
   
   对于提交节点(我在提交步骤中计划我的任务)。
   
    MSDN: CustomActionData Property
   
  
  
   最后,您需要访问任务调度API,或者使用
   
    Process.Start
   
   打电话
   
    schtasks.exe
   
   . API将为您提供更加无缝和健壮的体验,但是我使用了schtasks路由,因为我有方便的命令行。
  
  
   这是我最终得到的代码。我将它注册为用于安装、提交和卸载的自定义操作。
  
  using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Security.Permissions;
using System.Diagnostics;
using System.IO;
namespace MyApp
{
    [RunInstaller(true)]
    public partial class ScheduleTask : System.Configuration.Install.Installer
    {
        public ScheduleTask()
        {
            InitializeComponent();
        }
        [SecurityPermission(SecurityAction.Demand)]
        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
            RemoveScheduledTask();
            string installationPath = Context.Parameters["DIR"] ?? "";
            //Without the replace, results in c:\path\\MyApp.exe
            string executablePath = Path.Combine(installationPath, "MyApp.exe").Replace("\\\\", "\\");
            Process scheduler = Process.Start("schtasks.exe",string.Format("/Create /RU SYSTEM /SC HOURLY /MO 2 /TN \"MyApp\" /TR \"\\\"{0}\\\"\" /st 00:00", executablePath));
            scheduler.WaitForExit();
        }
        [SecurityPermission(SecurityAction.Demand)]
        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
            RemoveScheduledTask();
        }
        private void RemoveScheduledTask() {
            Process scheduler = Process.Start("schtasks.exe", "/Delete /TN \"MyApp\" /F");
            scheduler.WaitForExit();
        }
    }
}