I am creating an application that creates a scheduled task for every user on first logon. I am using NuGet package Task Scheduler Managed Wrapper 2.5.21. When the exe run on logon, the Access Denied error occurs. When manually run the exe as Administrator, the Scheduled Task is created. How can I overcome this issue?
string installPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
using (TaskService ts = new TaskService())
{
TaskDefinition td = ts.NewTask();
td.Actions.Add(new ExecAction("MyExe.exe", null, installPath));
td.Triggers.Add(new SessionStateChangeTrigger
{
StateChange = TaskSessionStateChangeType.SessionUnlock,
UserId = Environment.UserName
});
td.Principal.RunLevel = TaskRunLevel.Highest;
td.Principal.LogonType = TaskLogonType.InteractiveToken;
ts.RootFolder.RegisterTaskDefinition("task_" + Environment.UserName, td);
}