Vista Schedule Task From Setup

后端 未结 2 1697
自闭症患者
自闭症患者 2021-01-01 02:46

I\'m deploying a C# application using the Setup Wizard project in Visual Studio 2008.

What is the simplest way for me to have Windows schedule my application to ru

相关标签:
2条回答
  • 2021-01-01 02:57

    Scheduled task is your way to go. Take a look at this page for how to setup the task with a script.

    0 讨论(0)
  • 2021-01-01 03:06

    This took some putting together for me, so here's complete documentation for scheduling a task from a setup project.

    Once you have your deployment project created, you're going to need to use Custom Actions to schedule the task. Walkthrough: Creating a Custom Action

    Note: The walkthrough asks you to add the primary output to the Install node, even if you don't plan on doing anything custom during the Install step. This is important, so don't ignore it like I did. The Installer Class does some state management during this step, and needs to run.

    The next step is to pass the installation directory to the custom action. This is done via the CustomActionData property. I entered /DIR="[TARGETDIR]\" for the commit node (I schedule my task during the commit step). MSDN: CustomActionData Property

    Finally, you'll need to either access the task scheduling API, or use Process.Start to call schtasks.exe. The API will give you a more seamless and robust experience, but I went with the schtasks route because I had the command line handy.

    Here is the code I ultimately ended up with. I registered it as a custom action for install, commit, and uninstall.

    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();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题