what is the best way to deploy task scheduler with my .exe ?

Topics: Errors
Oct 19, 2014 at 9:01 AM
I want to register a task in client system at the time of installation ...
I combined it with installer class .. but when running installer its give exception UserId: , Task:
I already run it as Admin
It is giving exception on line when register task.. i.e

ts.RootFolder.RegisterTaskDefinition("Test", td);

Please Help Me!
Thank you
Coordinator
Oct 19, 2014 at 4:04 PM
Will you elaborate on the exception you are getting and the trigger and action you have added to the task?
Oct 20, 2014 at 7:32 AM
public override void Install(IDictionary mySavedState)
    {
        try
        {
            using (TaskService ts = new TaskService())
            {
                // Create a new task definition and assign properties
                TaskDefinition td = ts.NewTask();
                td.RegistrationInfo.Description = "Hello";
                td.Settings.RestartCount = 50;

                td.Settings.RestartInterval = TimeSpan.FromMinutes(1);
               // td.Principal.UserId = string.Concat(Environment.UserDomainName, "\\", Environment.UserName);
                td.Principal.RunLevel = TaskRunLevel.Highest;
                td.Settings.DisallowStartIfOnBatteries = false;
                td.Settings.RunOnlyIfIdle = false;
                td.Settings.RunOnlyIfNetworkAvailable = false;
                td.Settings.StopIfGoingOnBatteries = false;
                td.Settings.StartWhenAvailable = true;
                td.Settings.WakeToRun = true;
                td.Settings.Enabled = true;
                td.Settings.AllowHardTerminate = false;
                td.Settings.IdleSettings.StopOnIdleEnd = false;
                td.Principal.LogonType = TaskLogonType.S4U;
                td.Triggers.Add((new DailyTrigger { DaysInterval = 10 }));

                td.Actions.Add(new ExecAction("notepad.exe", null, null));

                // Register the task in the root folder
                ts.RootFolder.RegisterTaskDefinition("Test", td);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }
Oct 20, 2014 at 7:43 AM
when i am running above code in installer class its giving error (18,8)UserId:
if i am going to provide UserId: in
ts.RootFolder.RegisterTaskDefinition("Test", td,TaskCreation.CreateOrUpdate,string.Concat(Environment.UserDomainName, "\", Environment.UserName),null,TaskLogonType.S4U,null);

again error
(20,8)UserId:

Thanks
Coordinator
Oct 24, 2014 at 2:28 PM
Check this response to see if it helps: https://taskscheduler.codeplex.com/discussions/370130
Marked as answer by dahall on 9/1/2015 at 10:04 AM