我想在我的程序安装中使用Inno Setup,我需要Inno Setup在Windows任务计划程序中创建一个任务,以便在每次互联网连接时启动program.exe
。
我可以do this manually,但我希望Inno Setup通过Schtasks
command执行此操作。
这是我的创新设置代码(here):
#define MyAppName "Desktop"
#define MyAppVersion "2"
#define MyAppPublisher "MH"
#define MyAppExeName "Desktop.exe"
[Setup]
AppId={{EFBBA2D3-C6F0-4D3D-BBD5-5AF126C3E8E9}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppPublisher={#MyAppPublisher}
DefaultDirName={pf}\{#MyAppName}
DefaultGroupName={#MyAppName}
OutputBaseFilename=setup 2
Compression=lzma
SolidCompression=yes
PrivilegesRequired=admin
[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"
[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
[Files]
Source: "E:\IdeaProjects\Desktop\Desktop.exe"; DestDir: "{app}"; Flags: ignoreversion
[Icons]
Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{group}\{cm:UninstallProgram,{#MyAppName}}"; Filename: "{uninstallexe}"
Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
Filename: "schtasks.exe"; \
Parameters: "/Create /TN MyTask /XML ""{tmp}\Task.xml"""; \
StatusMsg: "Scheduling task..."; BeforeInstall: CreateTaskXml
;Flags: runhidden;
[Code]
procedure CreateTaskXml;
var
TaskXml: string;
begin
TaskXml :=
'<?xml version="1.0"?>' + #13#10 +
'<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">'
+ #13#10 +
' <Triggers>' + #13#10 +
{ The EventTrigger here }
' <EventTrigger>' +
' <Enabled>true</Enabled>' +
' <Subscription><QueryList><Query Id="0" Path="Microsoft-Windows-NetworkProfile/Operational"><Select Path="Microsoft-Windows-NetworkProfile/Operational">*[System[Provider[@Name='Microsoft-Windows-NetworkProfile'] and EventID=10000]]</Select></Query></QueryList></Subscription>' +
' </EventTrigger>' +
' </Triggers>' + #13#10 +
{ ... }
' <Actions Context="Author">' + #13#10 +
' <Exec>' + #13#10 +
' <Command>' + ExpandConstant('{app}\{#MyAppExeName}') + '</Command>' + #13#10 +
' </Exec>' + #13#10 +
' </Actions>' + #13#10 +
'</Task>' + #13#10;
if SaveStringToFile(ExpandConstant('{tmp}\Task.xml'), TaskXml, False) then
begin
Log('Task XML successfully created');
end
else
begin
Log('Failed to create task XML');
end;
end;
你能帮帮我吗?感谢。
答案 0 :(得分:1)
与任何其他模糊的计划设置一样,您必须使用任务的XML定义。
最简单的方法是在Windows任务计划程序GUI中配置任务,选择已创建的任务并选择导出命令。
它将创建任务的XML定义,其中包含以下内容:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<!-- ... -->
<Triggers>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription><QueryList><Query Id="0" Path="Microsoft-Windows-NetworkProfile/Operational"><Select Path="Microsoft-Windows-NetworkProfile/Operational">*[System[Provider[@Name='NetworkProfile'] and EventID=10000]]</Select></Query></QueryList></Subscription>
</EventTrigger>
</Triggers>
<!-- ... -->
</Task>
编辑XML以删除所有内容,特定于您的计算机。
然后使用导出的XML作为文件的模板,您将在安装程序中动态创建文件,其中包含已安装应用程序的实际路径:
[Run]
Filename: "schtasks.exe"; \
Parameters: "/Create /TN MyTask /XML ""{tmp}\Task.xml"""; \
StatusMsg: "Scheduling task..."; Flags: runhidden; BeforeInstall: CreateTaskXml
[Code]
procedure CreateTaskXml;
var
TaskXml: string;
begin
TaskXml :=
'<?xml version="1.0"?>' + #13#10 +
'<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">'
+ #13#10 +
' <Triggers>' + #13#10 +
{ The EventTrigger here }
' </Triggers>' + #13#10 +
{ ... }
' <Actions Context="Author">' + #13#10 +
' <Exec>' + #13#10 +
' <Command>' + ExpandConstant('{app}\MyProg.exe') + '</Command>' + #13#10 +
' </Exec>' + #13#10 +
' </Actions>' + #13#10 +
'</Task>' + #13#10;
if SaveStringToFile(ExpandConstant('{tmp}\Task.xml'), TaskXml, False) then
begin
Log('Task XML successfully created');
end
else
begin
Log('Failed to create task XML');
end;
end;
CreateTaskXml
中的SaveStringToFile
function以Ansi编码创建XML。如果您需要Unicode,则必须使用UTF-16编码创建XML(schtasks
不支持UTF-8)。