Wednesday, October 5, 2011

Code snippet: how to use Windows Task Scheduler from .NET

First, make a reference to TaskScheduler 1.1 Type Library in COM components.

Then:

using System;
using System.IO;
using System.Threading;

using TaskScheduler;

namespace TaskScheduleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                if (args[0].Equals("bing"))
                {
                    for (int i = 0; i < 10; ++i)
                    {
                        Console.WriteLine("Bing!");
                        Thread.Sleep(1000);
                    }

                    return;
                }
                else if (args[0].Equals("install"))
                {
                    Install();
                    return;
                }
                else if (args[0].Equals("remove"))
                {
                    Remove();
                    return;
                }
            }


            Console.Error.WriteLine("Usage: {0} {bing|install|remove}");
        }


        static private void Remove()
        {
            ITaskService service = new TaskSchedulerClass();
            service.Connect(null, null, null, null);
            ITaskFolder folder = service.GetFolder(@"\");
            try
            {
                folder.DeleteTask(@"\Microsoft\Bing\Bing", 0);
                folder.DeleteFolder(@"\Microsoft\Bing", 0);
            }
            catch (FileNotFoundException)
            {
            }
            catch (DirectoryNotFoundException)
            {
            }
            catch (System.Runtime.InteropServices.COMException cex)
            {
                // Something else is still there

                if ((uint)cex.ErrorCode != 0x80070091)
                    Console.Error.WriteLine("Failed to remove task: {0}", cex.ErrorCode);
            }
        }


        static private void Install()
        {
            ITaskService service = new TaskSchedulerClass();
            service.Connect(null, null, null, null);

            ITaskFolder folder = service.GetFolder(@"\");
            ITaskFolder bing = null;
            try
            {
                bing = folder.GetFolder(@"Microsoft\Bing");
            }
            catch (FileNotFoundException)
            {
            }


            if (bing == null)
                bing = folder.CreateFolder(@"Microsoft\Bing",

                    "D:(A;;FA;;;BA)(A;;FA;;;SY)(A;;GR;;;WD)");

            ITaskDefinition td = service.NewTask(0);
            td.RegistrationInfo.Description = "Bing!";
            td.Settings.Enabled = true;
            td.Principal.LogonType = _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN;


            IExecAction action = td.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC)
                as IExecAction;
            string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;
            action.Path = executable;
            action.WorkingDirectory = Path.GetDirectoryName(executable);
            action.Arguments = "bing";


            ITrigger trigger = td.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME);
            trigger.StartBoundary = DateTime.Now.AddMinutes(1).ToString("o");
            trigger.Enabled = true;


            bing.RegisterTaskDefinition(@"Bing", td, 6, null, null,
                _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, null);
        }
    }
}

No comments: