Click or drag to resize
Task Scheduler Managed Class Library

TaskFolderRegisterTask Method

Registers (creates) a new task in the folder using XML to define the task.

Namespace:  Microsoft.Win32.TaskScheduler
Assembly:  Microsoft.Win32.TaskScheduler (in Microsoft.Win32.TaskScheduler.dll) Version: 2.10.1
Syntax
public Task RegisterTask(
	string path,
	string xmlText,
	TaskCreation createType = TaskCreation.CreateOrUpdate,
	string userId = null,
	string password = null,
	TaskLogonType logonType = TaskLogonType.S4U,
	string sddl = null
)
Request Example View Source

Parameters

path
Type: SystemString
The task name. If this value is NULL, the task will be registered in the root task folder and the task name will be a GUID value that is created by the Task Scheduler service. A task name cannot begin or end with a space character. The '.' character cannot be used to specify the current task folder and the '..' characters cannot be used to specify the parent task folder in the path.
xmlText
Type: SystemString
An XML-formatted definition of the task.
createType (Optional)
Type: Microsoft.Win32.TaskSchedulerTaskCreation
A union of TaskCreation flags.
userId (Optional)
Type: SystemString
The user credentials used to register the task.
password (Optional)
Type: SystemString
The password for the userId used to register the task.
logonType (Optional)
Type: Microsoft.Win32.TaskSchedulerTaskLogonType
A TaskLogonType value that defines what logon technique is used to run the registered task.
sddl (Optional)
Type: SystemString
The security descriptor associated with the registered task. You can specify the access control list (ACL) in the security descriptor for a task in order to allow or deny certain users and groups access to a task.

Return Value

Type: Task
A Task instance that represents the new task.
Examples
C#
// Define a basic task in XML
var xml = "<?xml version=\"1.0\" encoding=\"UTF-16\"?>" +
   "<Task version=\"1.2\" xmlns=\"http://schemas.microsoft.com/windows/2004/02/mit/task\">" +
   "  <Principals>" +
   "    <Principal id=\"Author\">" +
   "      <UserId>S-1-5-18</UserId>" +
   "    </Principal>" +
   "  </Principals>" +
   "  <Triggers>" +
   "    <CalendarTrigger>" +
   "      <StartBoundary>2017-09-04T14:04:03</StartBoundary>" +
   "      <ScheduleByDay />" +
   "    </CalendarTrigger>" +
   "  </Triggers>" +
   "  <Actions Context=\"Author\">" +
   "    <Exec>" +
   "      <Command>cmd</Command>" +
   "    </Exec>" +
   "  </Actions>" +
   "</Task>";
// Register the task in the root folder of the local machine using the SYSTEM account defined in XML
TaskService.Instance.RootFolder.RegisterTaskDefinition("Test", xml);
See Also