Programmatically create a task:

using Microsoft.Win32.TaskScheduler; using (var ts = new TaskService(host, "администратор", "local", "pass")) { TaskDefinition td = ts.NewTask(); var tr = new BootTrigger() { Enabled = true }; td.RegistrationInfo.Date = DateTime.Now; td.RegistrationInfo.Author = "Index Admin"; td.RegistrationInfo.Description = "Создано Index Admin"; td.Principal.RunLevel = TaskRunLevel.Highest; td.Principal.UserId = "SYSTEM"; td.Principal.LogonType = TaskLogonType.ServiceAccount; td.Settings.MultipleInstances = TaskInstancesPolicy.IgnoreNew; td.Settings.DisallowStartIfOnBatteries = true; td.Settings.Hidden = false; td.Settings.Enabled = true; td.Settings.IdleSettings.RestartOnIdle = false; td.Settings.IdleSettings.StopOnIdleEnd = false; td.Settings.RunOnlyIfIdle = false; td.Settings.RunOnlyIfNetworkAvailable = false; td.Settings.StopIfGoingOnBatteries = true; td.Actions.Add(new ExecAction(TaskTarget, WorkDirectory)); td.Triggers.Add(tr); ts.RootFolder.RegisterTaskDefinition(staleName, td); } 

The question how to ask is not the root directory (not in the "/" scheduler), but arbitrary.

And is it possible to immediately start task from this instance?

  • one
    Absolute way there does not fit? - nick_n_a
  • Damn, friend, for sure. What I got to this RootFolder. ts.RootFolder.RegisterTaskDefinition($@"Stale\{staleName}", td); - for sure! Thank. - Vlad Chekunaev

0