Vista planning task from installation

I am deploying a C # application using the setup wizard project in Visual Studio 2008.

What is the easiest way for me to have Windows plan for my application to run at regular intervals (e.g. every 8 hours)? I prefer if this planning happens during the installation of the application, in order to simplify the configuration for the end user.

Thanks!

+5
source share
2 answers

A planned task is your way. Take a look at this page to configure the task using a script .

0
source

, .

. .

.. Install node, - . , , . Installer .

- . CustomActionData. /DIR="[TARGETDIR]\" node ( ). MSDN: CustomActionData

, 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();
        }
    }
}
+10

All Articles