Creating a job that runs on a specific week day at a specific time

Jun 18, 2008 at 2:47 PM
I  need to create a job that  runs  on a specific week day (say every tuesday ) at  a specific time (at 4 P.M.) . Moreover, i want  that  job should run with the credentails of  currently logged on user  OR  in other words....i don't want  to ask from the user his/her credentials . I tried doing this but unable to do it . One thing i am not getting is that  how to specify the time(like 4 P.M. mentioned above) in a weekly trigger .  Please help me out .
Coordinator
Jul 18, 2008 at 5:39 PM
Edited Jul 18, 2008 at 5:40 PM

 

// Get the service on the local machine
TaskService ts = new TaskService();

 

 

 

// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description =
"Does something";

 

 // Set the principle to be the interactive user
td.Principal.LogonType =

 

TaskLogonType.InteractiveToken;

 

 

 

// Create a weekly trigger that will run every Tuesday
WeeklyTrigger
weeklyTrigger = new WeeklyTrigger();
weeklyTrigger.DaysOfWeek =
DaysOfTheWeek.Tuesday;
weeklyTrigger.WeeksInterval = 1;

 

 // Start the trigger from today at 4pm onward (DateTime.Today starts at midnight so adding 16 hours makes it 4pm)
weeklyTrigger.StartBoundary = 

 

DateTime.Today.AddHours(16);
td.Triggers.Add(weeklyTrigger);

 

 

 

// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));

 

 

 

// Register the task in the root folder 
ts.RootFolder.RegisterTaskDefinition(
"Test", td);