Accessing Windows Scheduled Task using C#

北城余情 提交于 2019-11-28 09:30:57

Someone has written a task scheduler class library on codeproject.com, it might be what your after...

:)

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.

You must call RegisterTaskDefintion for the task's definition with the new username and password to change just the password.

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

Check out this library for working with TaskSheduler. It's written in VB, but I referenced it easily and called it from C#.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!