System.IO.FileNotFoundException, The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
at Microsoft.Win32.TaskScheduler.V2Interop.ITaskFolder.RegisterTaskDefinition(String Path, ITaskDefinition pDefinition, Int32 flags, Object UserId, Object password, TaskLogonType LogonType, Object sddl)
at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String Path, TaskDefinition definition, TaskCreation createType, String UserId, String password, TaskLogonType LogonType, String sddl)
Used .NET-Framework 2.0 - Version 2.3.4.0
In fact I'm using the TaskCreation.CreateOrUpdate enum value and the process is elevated. Let me give you some more information:
I'm using a RegistrationTrigger, with a delay of 8 seconds and a EndBoundary of 20 seconds.
* RunLevel is TaskRunLevel.LUA
* DeleteExpiredTaskAfter is 40 seconds
* Principal.UserId is the UserName of the Win32_ComputerSystem ManagementObject (Example: WIN-GVLEGB4ESV6\Test )
```
using System;
using Microsoft.Win32.TaskScheduler;
using System.Management;
namespace TestTaskScheduler
{
class Program
{
static void Main(string[] args)
{
var ts = new TaskService();
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "some description";
td.Principal.LogonType = TaskLogonType.InteractiveToken;
var computerSystemClass = new ManagementClass("Win32_ComputerSystem");
var computerSystems = computerSystemClass.GetInstances();
var enumerator = computerSystems.GetEnumerator();
while (enumerator.MoveNext())
{
var computerSystem = enumerator.Current;
td.Principal.UserId = (string)computerSystem["UserName"];
}
td.Actions.Add(new ExecAction("cmd.exe", "-someparameter"));
// Create Trigger
var trigger = new RegistrationTrigger { Enabled = true };
trigger.Delay = TimeSpan.FromSeconds(8);
trigger.EndBoundary = DateTime.Now + TimeSpan.FromSeconds(20);
td.Triggers.Add(trigger);
TaskFolder tf = ts.RootFolder;
td.Principal.RunLevel = TaskRunLevel.LUA;
td.Settings.StartWhenAvailable = true;
td.Settings.Hidden = false;
td.Settings.MultipleInstances = TaskInstancesPolicy.StopExisting;
td.Settings.DisallowStartIfOnBatteries = false;
td.Settings.StopIfGoingOnBatteries = false;
td.Settings.IdleSettings.StopOnIdleEnd = false;
td.Settings.DeleteExpiredTaskAfter = TimeSpan.FromSeconds(40);
TaskFolder testFolder = null;
foreach (TaskFolder taskFolder in tf.SubFolders)
{
if (taskFolder.Name.Equals("TEST", StringComparison.OrdinalIgnoreCase))
{
testFolder = taskFolder;
}
}
if (testFolder == null)
testFolder = tf.CreateFolder("TEST");
testFolder.RegisterTaskDefinition("Start", td, TaskCreation.CreateOrUpdate, null, null, TaskLogonType.InteractiveToken);
Console.ReadKey();
}
}
}
```
Comments: ** Comment from web user: dahall **
I have run this code with repeated success on Win10 build 10159 and the assemblies I posted here (v2.3.5 beta). Please confirm this works for you.
```
string taskName = "Start", taskFolder = "Test";
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "some description";
// No need to set td.Principal.LogonType (done in registration)
// No need to set td.Principal.UserId with InteractiveToken
td.Actions.Add("cmd.exe", "-someparameter");
td.Triggers.Add(new RegistrationTrigger { Delay = TimeSpan.FromSeconds(8),
EndBoundary = DateTime.Now + TimeSpan.FromSeconds(20) });
td.Settings.StartWhenAvailable = true;
td.Settings.MultipleInstances = TaskInstancesPolicy.StopExisting;
td.Settings.DisallowStartIfOnBatteries = false;
td.Settings.StopIfGoingOnBatteries = false;
td.Settings.IdleSettings.StopOnIdleEnd = false;
td.Settings.DeleteExpiredTaskAfter = TimeSpan.FromSeconds(5);
// CreateFolder will now create or retrieve the folder with these parameters
TaskFolder testFolder = ts.RootFolder.CreateFolder(taskFolder, null, false);
var t = testFolder.RegisterTaskDefinition(taskName, td,
TaskCreation.CreateOrUpdate, null, null,
TaskLogonType.InteractiveToken);
```