access denied when create task schedule in c#

0

i want to create a schedule task in root folder . this is my code

using (TaskService ts = new TaskService())
{
    TaskDefinition td = ts.NewTask();
    td.RegistrationInfo.Description = "notepad";
    td.Principal.UserId = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    td.Principal.LogonType = TaskLogonType.InteractiveToken;
    td.Triggers.Add(new LogonTrigger() { Enabled = true });
    td.Principal.RunLevel = TaskRunLevel.Highest;
    td.Settings.DisallowStartIfOnBatteries = false;
    td.Settings.StopIfGoingOnBatteries = false;
    td.Settings.ExecutionTimeLimit = TimeSpan.Zero;            

    td.Actions.Add(new ExecAction("notepad.exe", null, null));
    // Register the task in the root folder
    ts.RootFolder.RegisterTaskDefinition(@"Browser", td);
}

The application gives "access denied", even though my user is administrator!

Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

enter image description here

c#
scheduled-tasks
asked on Stack Overflow Apr 25, 2018 by khosravi • edited Apr 25, 2018 by khosravi

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0