Search code examples
xmlinno-setupwindows-task-scheduler

Create XML task file with installation path before running schtask.exe in Inno Setup


I'm trying create Inno Setup with scheduled task from XML file. The scheduled task is: My Application need to start with user login.

in Inno Setup script:

[Run]
Filename: "schtasks.exe"; 
    \Parameters: "/create /XML ""{app}\Schedule.xml"" /TN AppStart"

in Schedule.xml file:

<Actions Context="Author">
    <Exec>
        <Command>"C:\Program Files\MyApp\MyApp.exe"</Command>
    </Exec>
</Actions>

This works correctly. But I'd like to set the application path in XML file as {app}\MyApp.exe, because user can install it any location. How can I change this path in the XML file in the setup's run time?


Solution

  • Use the /TR switch, instead of using the XML to specify the path to run.

    [Run]
    Filename: "schtasks.exe"; \
        Parameters: "/Create /TR ""{app}\MyApp.exe"" /TN AppStart"
    

    If you insist on using XML for some reason, you have to create the file on the fly.

    [Run]
    Filename: "schtasks.exe"; \
        Parameters: "/Create /XML ""{tmp}\Schedule.xml"""; \
        BeforeInstall: CreateScheduleXML
    
    [Code]
    
    procedure CreateScheduleXML;
    var
      FileName: string;
      AppPath: string;
    begin
      FileName := ExpandConstant('{tmp}\Schedule.xml');
      AppPath := ExpandConstant('{app}\MyApp.exe');
      { Create file here }
    end;
    

    You can create the file using simple functions like the SaveStringsToUTF8File or use the MSXML2.DOMDocument COM object (see Edit installed XML file according to user preferences in Inno Setup).