Task scheduler by using Microsoft.Win32.TaskScheduler.dll

AnilkumarM 41 Reputation points
2021-03-30T10:53:25.597+00:00

I am creating App to create a Task scheduler automatically in C#. I am struggling to set the date in a month and a week in a day(see below code). I am unable to set the "New MonthlyTrigger" "DaysOfMonth" and "DaysOfWeek" as int. I am getting an error as below snap.

using Microsoft.Win32.TaskScheduler;  
using System;  
using System.Globalization;  
using System.Windows.Forms;  
  
namespace WindowsFormsApp1  
{  
    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
        }  
        private void button34_Click(object sender, EventArgs e)  
        {  
            TaskService ts = new TaskService();  
            TaskDefinition td = ts.NewTask();  
            td.RegistrationInfo.Description = "New task create";  
            td.RegistrationInfo.Author = "Company";  
            CultureInfo cultinfo = new CultureInfo("en-US");  
            DateTime startDate = Convert.ToDateTime("04/01/2021 12:10:15 AM", cultinfo);  
            DateTime EndDate = Convert.ToDateTime("04/01/2022 12:10:15 AM", cultinfo);  
            td.Triggers.Add(new MonthlyTrigger  
            {  
                StartBoundary = startDate,  
                MonthsOfYear = MonthsOfTheYear.AllMonths,  
                DaysOfMonth = 1,  
                EndBoundary = EndDate,  
                Enabled = true  
            });  
            td.Triggers.Add(new WeeklyTrigger  
            {  
                StartBoundary = startDate,  
                DaysOfWeek = 1,  
                EndBoundary = EndDate,  
                Enabled = true  
            });  
            td.Actions.Add(new ExecAction("notepad.exe", "C:\\Anilkumar\\Testing API\\test.log", null));  
            ts.RootFolder.RegisterTaskDefinition(@ "Test", td);  
        }  
    }  
}  

82658-int.jpg

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,278 questions
{count} votes

Accepted answer
  1. Castorix31 81,741 Reputation points
    2021-03-30T12:18:55.79+00:00

    I have adapted one of my test codes by adding MonthlyTrigger, using TaskScheduler 1.1 Type Library (there is no DaysOfWeek, only DaysOfMonth)

    This test works on my Windows 10 OS =>

                // To check the task : "C:\WINDOWS\system32\mmc.exe" "C:\WINDOWS\system32\taskschd.msc" /s  
    
                // Add reference to : TaskScheduler 1.1 Type Library  
                // Add : using TaskScheduler;  
                ITaskService ts = new TaskScheduler.TaskScheduler();  
                try  
                {  
                    ts.Connect(null, null, null, null);  
                }  
                catch (Exception ex)  
                {  
                    System.Windows.Forms.MessageBox.Show("Error : " + ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
                    return;  
                }  
                ITaskDefinition td = ts.NewTask(0);  
                td.RegistrationInfo.Author = "Task Author";  
                td.RegistrationInfo.Description = "Task Description";  
    
                //ITimeTrigger tt = (ITimeTrigger)td.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME);  
                //tt.Enabled = true;  
                //tt.StartBoundary = (DateTime.Now.AddSeconds(60)).ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss");  
                //tt.Repetition.Interval = "PT1M";  
    
                CultureInfo cultinfo = new CultureInfo("en-US");   
                DateTime startDate = Convert.ToDateTime("04-01-2021 12:10:15 AM", cultinfo);  
                DateTime endDate = Convert.ToDateTime("04-01-2022 12:10:15 AM", cultinfo);  
    
                // https://learn.microsoft.com/en-us/windows/win32/taskschd/monthlytrigger  
                IMonthlyTrigger mt  = (IMonthlyTrigger)td.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_MONTHLY);  
                mt.StartBoundary = startDate.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss");    
                mt.EndBoundary = endDate.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss");  
                mt.DaysOfMonth = 0x20000000 | 0x40000000; // 30 and 31  
                //mt.MonthsOfYear = MonthsOfTheYear.AllMonths;  
                mt.MonthsOfYear = 0X400 | 0X800; // November and December  
    
                td.Settings.MultipleInstances = _TASK_INSTANCES_POLICY.TASK_INSTANCES_PARALLEL;                 
    
                IExecAction execAction = (IExecAction)td.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);  
                execAction.Path = @"c:\windows\notepad.exe";  
                var rootFolder = ts.GetFolder("\\");  
                try  
                {  
                    IRegisteredTask ticket = rootFolder.RegisterTaskDefinition("MyNewTask", td, (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null, _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, null);  
                }  
                catch (Exception ex)  
                {  
                    System.Windows.Forms.MessageBox.Show("Could not add a task : " + ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
                    return;  
                }  
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Timon Yang-MSFT 9,576 Reputation points
    2021-03-31T01:59:00.083+00:00

    It may be that some methods and properties have changed due to the change of the version.

    When I use Assembly Microsoft.Win32.TaskScheduler 2.9.1 to test, DaysOfMonth needs an array, and DaysOfWeek needs DaysOfTheWeek Enum.

    83025-2.png

    82988-3.png

    So please try:

      DaysOfMonth = new int[] { 1 },  
      DaysOfWeek = DaysOfTheWeek.Friday,  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Pranay Kumar Bakka 1 Reputation point
    2022-09-07T08:23:16.647+00:00

    Does this dll work for scheduling a task in windows xp os ?