在 Windows 任务计划程序中更改已计划任务的运行时间
我在修改机器上已存在的任务时遇到问题。我正在尝试使用 C# 生成的互操作接口(从 system32/taskschd.dll 生成的 Interop.TaskScheduler.dll)来执行此操作。
首先,我无法使用其他库,例如 http://taskscheduler.codeplex.com/。 已经测试过,它可以与前面提到的库一起使用。现在,当我尝试对生成的接口执行相同操作时,没有任何变化。基本上我正在做的事情:
string STR_DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
string taskName = "taskName",
user = "user",
pass = "pass";
DateTime nextRun = new DateTime.Now.AddDays(7);
TaskSchedulerClass ts = new TaskSchedulerClass();
ts.Connect(null, null, null, null);
IRegisteredTask task = ts.GetFolder("\\").GetTask(String.Format("\\{0}",taskName));
foreach (ITrigger t in task.Definition.Triggers)
t.StartBoundary = nextRun.ToString(STR_DateTimeFormat.Replace(" ", "T"));
ts.GetFolder("\\").RegisterTaskDefinition(task.Path,
task.Definition,
(int)_TASK_CREATION.TASK_UPDATE,
user,
pass,
_TASK_LOGON_TYPE.TASK_LOGON_PASSWORD,
null);
乍一看它应该可以工作,但是由于某种原因,当我尝试分配新的日期时间以在线运行时:
t.StartBoundary = nextRun.ToString(STR_DateTimeFormat.Replace(" ", "T"));
它不起作用。实际上,在该 foreach 中它已更改,但是当我尝试调试并创建另一个打印 StartBoundary 值的 foreach 时,它显示旧值。我做错了什么吗?有机会让它发挥作用吗? :-) 谢谢。
I have problem with modifying tasks which already exists on machine. I'm trying to do this with generated interop interfaces from C# (Interop.TaskScheduler.dll generated from system32/taskschd.dll).
To start with, I can't use other libraries like http://taskscheduler.codeplex.com/.
Already tested and it works with library mentioned before. Now when I try do same with generated interfaces nothing changes. Basically what I'm doing:
string STR_DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
string taskName = "taskName",
user = "user",
pass = "pass";
DateTime nextRun = new DateTime.Now.AddDays(7);
TaskSchedulerClass ts = new TaskSchedulerClass();
ts.Connect(null, null, null, null);
IRegisteredTask task = ts.GetFolder("\\").GetTask(String.Format("\\{0}",taskName));
foreach (ITrigger t in task.Definition.Triggers)
t.StartBoundary = nextRun.ToString(STR_DateTimeFormat.Replace(" ", "T"));
ts.GetFolder("\\").RegisterTaskDefinition(task.Path,
task.Definition,
(int)_TASK_CREATION.TASK_UPDATE,
user,
pass,
_TASK_LOGON_TYPE.TASK_LOGON_PASSWORD,
null);
For first look it should be working, but for some reason when I try to assign new datetime for run in line:
t.StartBoundary = nextRun.ToString(STR_DateTimeFormat.Replace(" ", "T"));
It doesn't work. Actually in that foreach it's changed but when I tried to debug and created another foreach which prints value of StartBoundary it shows old value. Am I doing something incorrect? Is there any chance to get it working? :-) Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您尝试像这样使用任务计划程序 C# API 更新任务,则需要声明一个新的 ITaskDefinition。如果您尝试更改现有定义然后重新注册,则不起作用。
答案归原发者所有,请参阅问题评论。我写这个答案是为了澄清问题,并提请注意这个问题已经得到解答,因为类似的问题困扰了我几天。
If you are attempting to update a task using the Task Scheduler C# API like this, you need to declare a new ITaskDefinition. If you attempt to change the existing definition and then re-Register, it does not work.
Answer credit goes to original poster, see comment on question. I wrote up this answer to clarify the issue, and draw attention to the fact that the question had been answered, as a similar issue troubled me for a few days.
克拉克·博斯
Clark Bohs
是的。您必须创建一个具有相同标题和文件夹的新任务定义才能更新现有任务定义。尝试注册它并在任务计划程序中查看该任务。它应该已经用新的时间表更新了任务。但是,历史应该保持不变。
Yes. You have to create a new task definition with the same title and folder to update the existing one. Try to register it and see the task in Task Scheduler. It should have updated the task with the new schedule. But, the history should remain the same.