This project has moved and is read-only. For the latest updates, please go here.

System.IO.DirectoryNotFoundException... What?

Topics: Errors
Aug 30, 2016 at 9:41 PM
Edited Aug 30, 2016 at 9:49 PM
Does this make any sense to you? System.IO.DirectoryNotFoundException, huh?
Exception Type: System.IO.DirectoryNotFoundException
Message: The system cannot find the path specified. (Exception from HRESULT: 0x80070003)

The exception occurred 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) in C:\Users\dahall\Documents\Visual Studio 2010\Projects\TaskService\TaskFolder.cs:line 430
   at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String Path, TaskDefinition definition) in C:\Users\dahall\Documents\Visual Studio 2010\Projects\TaskService\TaskFolder.cs:line 373
And yes, I make sure that my task folder exists before going ahead and attempting to make my task.

The code that I'm using is this...
Function doesTaskFolderExist() As Boolean
    Return TaskScheduler.TaskService.Instance.RootFolder.SubFolders.Exists("myTaskFolder")
  Catch ex As Exception
    Return False
  End Try
End Function

If doesTaskFolderExist() = False Then
  taskService.RootFolder.CreateFolder("my folder")
End If

taskService.RootFolder.SubFolders("myTaskFolder").RegisterTaskDefinition("myTask", newTask)
Aug 30, 2016 at 10:56 PM
The error is coming from the RegisterTaskDefinition native method. It likely has something to do with the settings for newTask.

To make the syntax simpler, try:
Dim tf = If(ts.GetFolder("X"), ts.RootFolder.CreateFolder("X"))
tf.RegisterTaskDefinition("myTask", newTask)
Aug 30, 2016 at 11:21 PM
Trying to find out why the code works on some machines and other it doesn't is turning into a real pain in the rear end.

Here's my code that I'm using. Like I said, it works on most machines with no issues but others... oops, something went wrong.
Dim taskService As New TaskService()
Dim newTask As TaskDefinition = taskService.NewTask
Dim logonTriggerDefinition As LogonTrigger = New LogonTrigger

newTask.RegistrationInfo.Description = "My Task"
newTask.Principal.RunLevel = TaskRunLevel.Highest
newTask.Principal.LogonType = TaskLogonType.InteractiveToken

Dim exePathInfo As New IO.FileInfo(Application.ExecutablePath)
newTask.Actions.Add(New ExecAction(exePathInfo.FullName, "", exePathInfo.DirectoryName))
exePathInfo = Nothing

With newTask.Settings
    .Compatibility = TaskCompatibility.V2
    .AllowDemandStart = True
    .DisallowStartIfOnBatteries = False
    .RunOnlyIfIdle = False
    .StopIfGoingOnBatteries = False
    .AllowHardTerminate = False
    .ExecutionTimeLimit = Nothing
End With

Catch ex As InvalidOperationException

    Functions.eventLogFunctions.writeToSystemEventLog("There was an error while validating the task definition settings.", EventLogEntryType.Error)

    MsgBox("There was an error while validating the task definition settings. Please see the Application Event Log for more details.", MsgBoxStyle.Critical, Me.Text)
    Exit Sub
End Try

Dim taskFolderObject As TaskFolder = If(taskService.GetFolder("myTaskFolder"), taskService.RootFolder.CreateFolder("myTaskFolder"))
taskFolderObject.RegisterTaskDefinition("My Task", newTask)

Sep 17, 2016 at 8:27 PM
Does this happen on systems that are Windows XP only or also on new versions of Windows? Windows XP does not support folders.

Also, LogonTriggers are tricky. If specified that way you have, without any UserId parameter, then it applies to all logons of all users on the system and must be registered with an account that has the rights on the system to specify that. Check the Documentation tab and look for security for more details.