question

AnilkumarM avatar image
0 Votes"
AnilkumarM asked TimonYang-MSFT edited

Task scheduler by using Microsoft.Win32.TaskScheduler.dll

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

dotnet-csharp
int.jpg (37.8 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Anilkumar-4568
When adding code, please click the Code Sample button and publish it so that members can see it more clearly.
83010-capture.png

0 Votes 0 ·
capture.png (6.1 KiB)
Castorix31 avatar image
1 Vote"
Castorix31 answered AnilkumarM commented

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://docs.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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you, I try this way.

0 Votes 0 ·
TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered TimonYang-MSFT edited

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.


2.png (14.5 KiB)
3.png (15.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.