使用 C# 访问 Windows 计划任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/992549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing Windows Scheduled Task using C#
提问by
How do I change the credentials used by a scheduled task using C#.NET?
如何使用 C#.NET 更改计划任务使用的凭据?
回答by
Someone has written a task scheduler class library on codeproject.com, it might be what your after...
有人在codeproject.com上写了一个任务调度程序类库,它可能是你想要的......
:)
:)
回答by Martin Konicek
Check out this libraryfor working with TaskSheduler. It's written in VB, but I referenced it easily and called it from C#.
查看此库以使用 TaskSheduler。它是用 VB 编写的,但我很容易引用它并从 C# 中调用它。
回答by Durgesh Gupta
Instead of using code, you can do it using 'SCHTASKS' command, run it using System.Diagnostic.Process.Start method with the parameters required. It's easy and not much effort required.
您可以使用“SCHTASKS”命令代替代码,使用 System.Diagnostic.Process.Start 方法和所需参数运行它。这很容易,不需要太多努力。
回答by grosser lacher
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