RegisterTaskDefinition failing with a System.IO.DirectoryNotFoundException, The system cannot find the path specified. (Exception from HRESULT: 0x80070003)

Nov 12, 2012 at 9:18 AM
Edited Nov 12, 2012 at 9:19 AM


I am getting an exception while creating a task using RegisterTaskDefinition. The stacktrace is as follows:

FUNCTION: Void RegisterTaskDefinition(Scheduler.Schedule, Microsoft.Win32.TaskScheduler.TaskDefinition, Microsoft.Win32.TaskScheduler.TaskCreation)ErrorDescriptionForBug: System.Runtime.InteropServices.COMException, Cannot create a file when that file already exists. (Exception from HRESULT: 0x800700B7)ErrorStackTrace:    at Scheduler.TaskSchedulerManager.RegisterTaskDefinition(Schedule schedule, TaskDefinition taskDefinition, TaskCreation taskCreation)



We create tasks in sub folder (Folder2) as shown below:

Root Folder:



After looking around i found that this exception occurs when Folder1 and Folder2 are not present in the system32\Tasks folder.

But we have no additional code for creating it as well.  In the RegisterTaskDefinition method we pass the path as 'Folder1\\Folder2' and they get created automatically if they are not present.  

One scenario in which the folder is not getting creating is when there is quota applied to the system installation drive.  But i checked the available space after quota being applied and it is more than 1GB.  Still the folder is not getting created.


I tried to use the TaskFolder.CreateFolder() method but once the folder is created it fails for the subsequent calls saying that cannot create a file that already exists.   Directory.Exists() always returns false even if the folders are present.

Is there any other option which can be used here?

Nov 12, 2012 at 4:02 PM

I would call the TaskService.GetFolder method and handle the FileNotFoundException as an indicator that the folder does not exist. I would then create it if necessary and handle those errors specifically.