Hi,
I'm getting errors when trying to create or delete a tasks for the current user. I have not been able to repo this on my machine at all. However, I can repo this on any test machine (i.e. no VS installed and user is not running with admin permissions).
System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) 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) at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String Path, TaskDefinition definition)
Here's my code:
using (var ts = new TaskService()) { var taskDef = ts.NewTask(); taskDef.RegistrationInfo.Description = string.Format( Resources.Scheduled_Task_Desc_Format, this.TargetEntity.FriendlyName ); taskDef.Triggers.Add(GetTrigger(_interval)); taskDef.Actions.Add( new ExecAction( Assembly.GetEntryAssembly().Location, "-s " + GetSyncFlowName(this.TargetEntity) ) ); ts.RootFolder.RegisterTaskDefinition(taskName, taskDef); }
for deletes
var taskFilter = new Regex( string.Concat("^", taskName, "$"), RegexOptions.Singleline | RegexOptions.Compiled ); // Find the existing instance of the task.. var olTask = ts.RootFolder.GetTasks(taskFilter).FirstOrDefault(); if (olTask != null) { ts.RootFolder.DeleteTask(taskName); }