简体   繁体   English

使用C#访问Windows计划任务

[英]Accessing Windows Scheduled Task using C#

如何使用C#.NET更改计划任务使用的凭据?

Instead of using code, you can do it using 'SCHTASKS' command, run it using System.Diagnostic.Process.Start method with the parameters required. 您可以使用“SCHTASKS”命令代替使用代码,使用System.Diagnostic.Process.Start方法运行它,并使用所需的参数。 It's easy and not much effort required. 这很容易,而且不需要太多努力。

Someone has written a task scheduler class library on codeproject.com , it might be what your after... 有人在codeproject.com上编写了一个任务调度程序类库,它可能是你的后...

:) :)

You must call RegisterTaskDefintion for the task's definition with the new username and password to change just the password. 您必须使用新的用户名和密码调用RegisterTaskDefintion以获取任务的定义,以仅更改密码。

Code fragment 代码片段

// Add COM-Reference to "TaskScheduler 1.1 Type Library" to the project
using TaskScheduler;

// code in function X

TaskSchedulerClass TaskClass = new TaskSchedulerClass();
TaskClass.Connect();

// access one task (or search for it or enumerate over all tasks)
IRegisteredTask lTask = null;
lTask = TaskClass.GetFolder("\\").GetTasks(0)[0];

// provide domain\\username and password (ask user for it, use encryption)
string lUsername = "TestDomain\\TestUsername"; // TestDomain can be the hostname for a local user
string lPassword = "xyzPassword";

RegisterTaskDefinition(lTask.Path, lTask.Definition, (int)_TASK_CREATION.TASK_UPDATE, lUsername, lPassword, lTask.Definition.Principal.LogonType, Type.Missing);

Original source for answer: http://taskscheduler.codeplex.com/discussions/215362 答案的原始来源: http//taskscheduler.codeplex.com/discussions/215362

Check out this library for working with TaskSheduler. 查看此库以使用TaskSheduler。 It's written in VB, but I referenced it easily and called it from C#. 它是用VB编写的,但我很容易引用它,并用C#调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM