C # - using scheduled tasks using Process.Start

I am trying to integrate a scheduled task into Process.Start

Process.Start("schtasks.exe", "\"" + textBox1.Text + "\""); 

How can I add the parameters below to the Process.Start instruction above?

 schtasks /Create /SC DAILY /TN TestJob /TR "C:\Program Files\test\test.exe 'C:\'" 
+4
source share
2 answers

You can interact directly with the Windows task manager using TaskScheduler . This will give you access to the entire spectrum of task properties and under what conditions it will be launched. This, of course, requires more code, but it gives you all the control you need in a controlled way.

This is a piece of code that I use myself and it works well (ive disabled some of my business logic, so not all arguments will compile / make sense). This will basically create a task that will run one minute from Now:

  TaskScheduler.TaskScheduler scheduler = new TaskScheduler.TaskScheduler(); scheduler.Connect(null, null, null, null); //run as current user. ITaskDefinition taskDef = scheduler.NewTask(0); taskDef.RegistrationInfo.Author = "Me me me"; taskDef.RegistrationInfo.Description = "My description"; taskDef.Settings.ExecutionTimeLimit = "PT10M"; // 10 minutes taskDef.Settings.DisallowStartIfOnBatteries = false; taskDef.Settings.StopIfGoingOnBatteries = false; taskDef.Settings.WakeToRun = true; ITimeTrigger trigger = (ITimeTrigger)taskDef.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME); DateTime nextRun = DateTime.Now.AddMinutes(1); // one minute from now trigger.StartBoundary = nextRun.ToString("s", System.Globalization.CultureInfo.InvariantCulture); IExecAction action = (IExecAction)taskDef.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC); action.Id = "exe name"; action.Path = "path to exe"; action.WorkingDirectory = "working dir"; action.Arguments = "app arguments"; /// <-- here you put your arguments.. ITaskFolder root = scheduler.GetFolder("\\"); IRegisteredTask regTask = root.RegisterTaskDefinition( "My task name", taskDef, (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, null, // user null, // password _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, //User must already be logged on. The task will be run only in an existing interactive session. "" //SDDL ); 

More detailed descriptions and code examples can be found here: Calling the Task Scheduler in Windows Vista (and Windows Server 2008) from managed code

+8
source

The problem is that you need to leave twice.

Custom command line command for:

 schtasks /Create /SC DAILY /TN TestJob /TR "C:\Program Files\test\test.exe 'C:\'" 

is an

 schtasks /Create /SC DAILY /TN TestJob /TR "\"C:\\Program Files\\test\\test.exe\" \"C:\\\"" 

So you need:

 Process.Start("schtasks.exe", string.Format(@"/Create /SC DAILY /TN TestJob /TR """"{0}"" ""{1}""""", textBox1.Text.Replace("\", "\\"), @"C:\")); 

(You do not have a compiler, so there may be typos, but you should get this idea.) I assume that the textBox contains the path to the exe, not sure where this parameter is from, or if it is hardcoded.

0
source

All Articles