Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# start a scheduled task

I'm trying to write a simple form in c# that will run a scheduled task one some computers. Whet I have so far is:

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 doesn't work (IE - the scheduled task didn't start). I tried running from cmd this:

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

and it worked fine. Any suggestions? THANKS!

like image 354
user1027429 Avatar asked Nov 03 '11 10:11

user1027429


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

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

I have used this one in the past to good effect.

like image 61
Oded Avatar answered Oct 11 '22 19:10

Oded


I use the following which works fine, may be of help (plugging in 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();
like image 24
dove Avatar answered Oct 11 '22 20:10

dove