Wednesday 28 September 2016

How to Enable, Disable and Start the task of task scheduler in C#.Net

Below function demonstrates how to enable, disable and run the already created scheduled task. It will first check if the task exists in task scheduler:

public static void updateUserTaskInScheduler(string action)
        {
            try
            {
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = "cmd.exe";
                startInfo.Arguments = "/C schtasks /query /TN <<TaskNameWithQuotes>>; //Check if task exists
                startInfo.RedirectStandardOutput = true;
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = true;
                startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                if (System.Environment.OSVersion.Version.Major < 6)
                {
                    startInfo.Verb = "runas";
                }
                using (Process process = Process.Start(startInfo))
                {
                    // Read in all the text from the process with the StreamReader.
                    using (StreamReader reader = process.StandardOutput)
                    {
                        string stdout = reader.ReadToEnd();
                        if (stdout.Contains("<<TaskName>>")) //If task exists
                        {
                            startInfo.RedirectStandardOutput = false;
                            startInfo.UseShellExecute = true;
                            switch (action)
                            {
                                case "Enable":
                                    startInfo.Arguments = "/C schtasks /Change /TN <<TaskNameWithQuotes>>  /Enable";
                                    break;

                                case "Disable":
                                    startInfo.Arguments = "/C schtasks /Change /TN <<TaskNameWithQuotes>> /Disable";
                                    break;

                                case "Run":
                                    startInfo.Arguments = "/C schtasks /RUN /TN <<TaskNameWithQuotes>> ";
                                    break;
                            }
                            Process.Start(startInfo).WaitForExit();
                        }
                        else
                        {
                             //Task doesnot exist
                        }
                        stdout = null;
                        reader.Close();
                        reader.Dispose();
                    }
                }
                startInfo = null;
            }
            catch (Exception ex)
            {
                    MessageBox.Show(ex.Message);
            }
        }


The above function can be called in the below manner:

1. To run the task:
            updateUserTaskInScheduler("Run");

2. To enable the task:
            updateUserTaskInScheduler("Enable");

3. To disable the task:
            updateUserTaskInScheduler("Disable");