This project has moved. For the latest updates, please go here.

XP vs Vista Task modification error

Oct 23, 2009 at 10:03 AM

Hi!

I tried to modify a task (trigger date), it works fine on Vista,

but on XP, the Task is deleted when I call RegisterTaskDefinition, and throws a FileNotFoundException.

I figured it out, that it modifies the task on XP, if I call it this way: (so I don't give the Path)

ts.RootFolder.RegisterTaskDefinition("", task.Definition);

but on Vista, this causes a new Task with a GUID like said in the RegisterTaskDefinition description.

Do you have any idea how to fix this?

Coordinator
Oct 27, 2009 at 4:56 AM

Make sure that the name does not include any characters other than numbers, letters, and spaces. Usually FileNotFoundExceptions are thrown when the 1.0 engine doesn't know how to persist the file. Especially make sure that the task name does not include any backslashes.

Oct 27, 2009 at 6:00 PM

Thanks, the task name does not include dots or something like this, but on Vista the path looks like:

task.Path = "\\TaskName",

and on XP:

task.Path = "C:\\Windows\\Tasks\\TaskName.job"

 

Coordinator
Oct 28, 2009 at 6:15 PM

I have run this code that uses the same COM library as on XP and it works as it should. Are you doing something different? Please include your code if this example doesn't help.

using (TaskService ts = new TaskService(null, null, null, null, true))
{
  // Create a new task definition and assign properties
  TaskDefinition td = ts.NewTask();
  td.RegistrationInfo.Description = "Does something";
  td.Principal.LogonType = TaskLogonType.InteractiveToken;

  // Create a trigger that will fire the task at this time every other day
  DailyTrigger dt = (DailyTrigger)td.Triggers.Add(new DailyTrigger { DaysInterval = 2 });
  dt.Repetition.Duration = TimeSpan.FromHours(4);
  dt.Repetition.Interval = TimeSpan.FromHours(1);

  // Create an action that will launch Notepad whenever the trigger fires
  td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));

  // Register the task in the root folder
  const string taskName = "Test";
  Task t = ts.RootFolder.RegisterTaskDefinition(taskName, td);
  System.Threading.Thread.Sleep(1000);
  Console.WriteLine("LastTime & Result: {0} ({1})", t.LastRunTime, t.LastTaskResult);

  // Retrieve the task, add a trigger and save it.
  t = ts.GetTask(taskName);
  td = t.Definition;
  td.Triggers[0].StartBoundary = DateTime.Today + TimeSpan.FromDays(7);
  ts.RootFolder.RegisterTaskDefinition(taskName, td);

  // Remove the task we just created
  Console.ReadKey(false);
  ts.RootFolder.DeleteTask(taskName);
}
Oct 29, 2009 at 7:29 AM

Thanks for the detailed code, here is mine, I only want to modify the start date of the task:

            TaskService ts = new TaskService();
            Task task = ts.GetTask("Test");
            TaskDefinition td = task.Definition;         
            td.Triggers[0].StartBoundary = date; // a DateTime
            ts.RootFolder.RegisterTaskDefinition(task.Path, td);

 

As I see, you didn't use the task.Path, instead you used the TaskName. I saw this task.Path in an example here in the Discussions,

maybe that will be the problem.

Also I think I must add some logontype, because it modifies the original, if I use the above code.

 

Coordinator
Nov 3, 2009 at 5:23 PM

Using the Path property for Task will return the folder path on Vista and higher, but returns the file path of the .job file on XP. The TaskFolder.RegisterTaskDefinition method expects a folder path only. On XP, since there are no subfolders, only a name is accepted. Using Task.Path is your problem. Use whichever variable you are using in GetTask.