C # run a scheduled task

I am trying to write a simple form in C # that will run a scheduled task on some computers. Whet I still:

private void button_Click(object sender, EventArgs e) { try { for (int i = 0; i < num_of_computers; i++) { string line; line = (" /run /tn myTask /s " + _ReplacerObj.MyComputers[i] + " /u user s /p password"); proc.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized; proc.FileName = @"C:\WINDOWS\SYSTEM32\schtasks.exe"; proc.Arguments = line; Process.Start(proc); } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error Message!"); } 

For some reason this does not work (IE - the scheduled task did not start). I tried working with cmd as follows:

 c:\windows\system32\schtasks.exe /run /tn myTask /s myIp /u user /p password 

and it worked fine. Any suggestions? THANKS!

+8
c # task
source share
3 answers

I suggest using one of the .NET wrappers for the task scheduler.

I have used this in the past for a good effect.

+6
source share

I use the following, which works great, can help (including your arguments)

 var p = new Process { StartInfo = { UseShellExecute = false, FileName = "SCHTASKS.exe", RedirectStandardError = true, RedirectStandardOutput = true, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, Arguments = arguments } }; p.Start(); 
0
source share

using Microsoft.Win32.TaskScheduler;

 using (TaskService tasksrvc = new TaskService(@"\\" + servername, username, domain, password, true)) { Task task = tasksrvc.FindTask(taskSchedulerName); task.Run(); } 
0
source share

All Articles