如何以编程方式计划任务 [英] How to Schedule a task programmatically

查看:56
本文介绍了如何以编程方式计划任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Google Updater之类的delphi 7安排任务?
我没有使用注册表,因为卡巴斯基反病毒软件将其检测为虚假警报.
我在注册表中作为启动项添加的所有内容都被检测为特洛伊木马,所以我决定使用任务计划

How can I schedule a task using delphi 7 like Google updater?
I'm not using the registry because it gets detected by Kaspersky antivirus as a false alarm.
Anything I add in the registry as a start-up item gets detected as a trojan so I decided to use task schedule

推荐答案

以下代码显示了如何删除和创建任务,该任务将在系统启动时以系统特权运行应用程序.它使用以下命令行:

The following piece of code shows how to delete and create the task which will run the application at system startup with system privileges. It uses the following command line:

但是,由于Windows Vista支持强制创建任务,因此任务计划程序不会将其用于与Windows XP的向后兼容性,因为Windows XP不存在此标志.因此,下面的示例尝试删除任务(如果已经存在),然后创建新任务.

However the Task Scheduler since Windows Vista supports force creation of tasks, I wouldn't use it for backward compatibility with Windows XP, where this flag doesn't exist. So the example below tries to delete the task (if already exists) and then create the new one.

它执行以下命令:

schtasks/delete/f/tn"myjob"
schtasks/create/tn"myjob"/tr"C:\ Application.exe"/sc ONSTART/ru系统"

schtasks /delete /f /tn "myjob"
schtasks /create /tn "myjob" /tr "C:\Application.exe" /sc ONSTART /ru "System"

/删除-删除任务
/f-取消确认
/create-创建任务参数
/tn-任务的唯一名称
/tr-可执行文件的文件名
/sc-计划类型,ONSTART-在启动时运行
/ru-在指定用户的权限下运行任务

/delete - delete the task
/f - suppress the confirmation
/create - create task parameter
/tn - unique name of the task
/tr - file name of an executable file
/sc - schedule type, ONSTART - run at startup
/ru - run task under permissions of the specified user

这是代码:

uses
  ShellAPI;

procedure ScheduleRunAtStartup(const ATaskName: string; const AFileName: string;
  const AUserAccount: string);
begin
  ShellExecute(0, nil, 'schtasks', PChar('/delete /f /tn "' + ATaskName + '"'),
    nil, SW_HIDE);
  ShellExecute(0, nil, 'schtasks', PChar('/create /tn "' + ATaskName + '" ' +
    '/tr "' + AFileName + '" /sc ONSTART /ru "' + AUserAccount + '"'),
    nil, SW_HIDE);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ScheduleRunAtStartup('myjob', 'C:\Application.exe', 'System');
end;

这篇关于如何以编程方式计划任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆