question

AbhinavMutreja-0310 avatar image
0 Votes"
AbhinavMutreja-0310 asked RLWA32-6355 commented

Access denied while deleting the task from Windows Task Scheduler in C#

I am trying to delete the task from the windows task scheduler in c# using below code -

using TaskService ts = new TaskService();
ts.RootFolder.DeleteTask(TaskName);

It is throwing below error. Kindly help.

Access is denied. (0x80070005 (E_ACCESSDENIED)) -- at Microsoft.Win32.TaskScheduler.V2Interop.ITaskFolder.DeleteTask(String Name, Int32 flags)

dotnet-csharp
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@AbhinavMutreja-0310
I created a new task and successfully deleted it with your code. It seems that there is nothing wrong with the code.
And according to the exception information, the reason for the error seems to be lack of permissions.
Have you ever tried to run your program with administrator permissions?

1 Vote 1 ·

Thanks @TimonYang-MSFT . Yes its an admin issue. I tested it with using admin rights and it worked fine.
I am packaging this executable(exe) inside a windows installer. What settings are needed in installer so that the package is installed without admin rights ? Also the when the executable runs it should also should not need admin rights to create task in windows task scheduler ?

Appreciate your help!

0 Votes 0 ·

@AbhinavMutreja-0310

What settings are needed in installer so that the package is installed without admin rights ?

I am not sure, I am not familiar with it.

Also the when the executable runs it should also should not need admin rights to create task in windows task scheduler

My guess is that whether you need administrator rights depends on whether your current user is in the administrator group.
My user is not an administrator user, no matter I create or delete Task (task scheduler or C#), I will not encounter permission problems.
But you may be an administrator user, so the task you create requires administrator privileges, but your program runs with non-administrator privileges by default, which causes the current problem.

0 Votes 0 ·

Does the installer write to any locations (e.g., C:\Program Files, HKLM registry) that require elevated privileges for access?

Does the scheduled task get created during the installation? If not, is it created by a standard user or by an Administrator?

How Is the executable that attempts to delete the scheduled task invoked? Directly by user interaction? During the install/uninstall process?

0 Votes 0 ·

1 Answer

Castorix31 avatar image
0 Votes"
Castorix31 answered

This works for me with
TaskScheduler 1.1 Type Library
=>

 ITaskService ts = new TaskScheduler.TaskScheduler();
 try
 {
     ts.Connect(null, null, null, null);
     ITaskFolder rootFolder = ts.GetFolder(string.Empty);
     rootFolder.DeleteTask("MyTask", 0);                   
 }
 catch (Exception ex)
 {
     System.Windows.Forms.MessageBox.Show("Error : " + ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     return;
 }


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.