dev.e.loper
dev.e.loper

Reputation: 36034

Running an executable programmatically .NET

I would like to execute a program in .NET server side code.

So far I have this:

    Process p = new Process();  
    p.StartInfo.FileName = "myProgram.exe";
    p.StartInfo.Arguments = " < parameter list here > ";
    p.Start();
    p.Close();

This is a console program. What happens is that console is opened and closed repeatedly without stopping.

Upvotes: 2

Views: 1836

Answers (3)

kay.one
kay.one

Reputation: 7692

You want,

Process p = new Process();  
    p.StartInfo.FileName = "myProgram.exe";
    p.StartInfo.Arguments = " < parameter list here > ";
    p.Start();
    p.WaitForExit();

what happens in your code is you start the process and you close it right away, what you need is call WaitForExit() which actually waits for the process to close on its own,

To Print the output before the app closes:

Process p = new Process();  
p.StartInfo.FileName = "myProgram.exe";
p.StartInfo.Arguments = " < parameter list here > ";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.WaitForExit();
Console.WriteLine(p.StandardOutput.ReadToEnd());

Upvotes: 3

driis
driis

Reputation: 164281

That code will not produce an infinite loop. It will fire up a program, and then immediately close the process. (You might not want to close it, but rather wait for the process to end).

Upvotes: 0

Josh Stodola
Josh Stodola

Reputation: 82483

Check out the BackgroundWorker class. Here is a more detailed walkthrough/example of its use.

Upvotes: 2

Related Questions