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

FileNotFoundException on WinXP

Sep 8, 2011 at 8:41 PM

I'm getting an error trying to create a task on WinXP. The task version is V1 and the code works fine on Win 7. However here is the error i get on XP:

System.IO.FileNotFoundException: Could not find file 'C:\WINDOWS\Tasks\Temp{31009e9f-e457-46d6-9619-4f04f4ac53ca}.job'.
File name: 'C:\WINDOWS\Tasks\Temp{31009e9f-e457-46d6-9619-4f04f4ac53ca}.job'
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
   at System.IO.File.OpenFile(String path, FileAccess access, SafeFileHandle& handle)
   at System.IO.File.SetLastWriteTimeUtc(String path, DateTime lastWriteTimeUtc)
   at Microsoft.Win32.TaskScheduler.TaskRegistrationInfo.set_Date(DateTime value)
   at RemoteInstaller.InstallConfig.ScheduleInstallation()

 

Here is the code I'm using to try and register the task:

 using (TaskService ts = new TaskService())
{
                    // Determine version and server state
                    Version ver = ts.HighestSupportedVersion;
                    bool newVer = (ver >= new Version(1, 2));

                    // Create a new task definition and assign properties
                    TaskDefinition td = ts.NewTask();
                    td.RegistrationInfo.Description = strDescription;

                    td.RegistrationInfo.Author = string.Format("{0}\\{1}", UserDomain, UserId);
                    td.RegistrationInfo.Date = DateTime.Now;

                    td.Settings.DisallowStartIfOnBatteries = false;
                    td.Settings.RunOnlyIfIdle = false;
                    td.Settings.RunOnlyIfNetworkAvailable = false;
                    td.Settings.StopIfGoingOnBatteries = false;

                    // Extra options if on Vista or Win7
                    if (newVer)
                    {

                        td.Settings.Compatibility = TaskCompatibility.V2;
                        td.Principal.RunLevel = TaskRunLevel.Highest;
                        td.Settings.DeleteExpiredTaskAfter = TimeSpan.FromDays(14);
                        td.Settings.MultipleInstances = TaskInstancesPolicy.IgnoreNew;
                        td.Settings.WakeToRun = true;
                        td.Settings.StartWhenAvailable = true;
                    }
                   
                    // Create a trigger that will launch at task at the time specified
                    TimeTrigger tt = new TimeTrigger();
                    tt.StartBoundary = datetimeScheduleTime;
                    tt.EndBoundary = datetimeScheduleTime.AddDays(1);
                    td.Triggers.Add(tt);

                    // Create an action that will executable this executable
                    td.Actions.Add(new ExecAction(Assembly.GetExecutingAssembly().Location, string.Format("/scheduled \"{0}\"", strConfigFile)));

                    // Register the task
                    if (newVer)
                    {
                       ts.RootFolder.CreateFolder("Test").RegisterTaskDefinition(
                            "TEST",
                            td,
                            TaskCreation.CreateOrUpdate,
                            string.Format(@"{0}\{1}", UserDomain, UserId),
                            UPPassword.DecryptString(UserPassword, (System.Security.Cryptography.RSACryptoServiceProvider)privateKey.PrivateKey),
                            TaskLogonType.Password);
                    }
                    else
                    {
                        ts.RootFolder.RegisterTaskDefinition(
                            @"TEST",
                            td,
                            TaskCreation.CreateOrUpdate,
                            null,
                            null,
                            TaskLogonType.InteractiveToken);
                    }
                    rtnValue = true;
}

Any help anyone can provide would be much appreciated.

Coordinator
Sep 9, 2011 at 12:18 AM

You have found a bug in that I have not handled this error condition. Effectively under V1, the RegistrationInfo.Date property is tied to the task's file last write time. If the task has not been created, that date can neither be set or retrieved. Of note is that when you register a V1 task the RegistrationInfo.Date property will then be available and will be set to the time the task was registered. For your code, I would do the following:

if (ts.HighestSupportedVersion >= new Version(1, 2))
   td.RegistrationInfo.Date = DateTime.Now;

I will put some error handling into the code for the next release so this property will return an exception that makes sense.

Sep 9, 2011 at 3:02 PM

Thanks for the quick response and solution. I really appreciate the the great library and fantastic support.