How to Launch an External Application in C#

Here is a C# example where one can execute a program using the Process Class. The example launches notepad with an argument of a filename to open and waits for completion. When the user closes notepad, its return code is printed out.

Console.WriteLine("Running");

Process pr = new Process();

pr.StartInfo.FileName = "Notepad.exe";
pr.StartInfo.Arguments = "test.dat";
pr.Start();

while (pr.HasExited == false)
    if ((DateTime.Now.Second % 5) == 0)
    { // Show a tick every five seconds.
        Console.Write(".");
        System.Threading.Thread.Sleep(1000);
    }

One can extract any output that a program does to the command line by looking at the Process.StandardOutput Property if it is applicable.

Share

9 Comments

  1. Eize says:

    Ouch this will create a busy loop for 4 seconds every time ..
    The 1 second sleep is only done once every 5 seconds ..
    You want to move the sleep outside the ‘if’

    while (pr.HasExited == false)
    {
    if ((DateTime.Now.Second % 5) == 0)
    { // Show a tick every five seconds.
    Console.Write(“.”);
    }
    System.Threading.Thread.Sleep(1000);
    }

  2. omegaman says:

    You are right! In my defense, this article was meant for demonstration purposes and was not optimized.

  3. Dotnetmania says:

    Great knowledge! Thanks for sharing this, it’s saved my time.

  4. Aksu says:

    Nice snippet! Including “using” statements would also be very helpful :)

  5. Hani says:

    It might be useful to know that Process is contained in System.Diagnostics

  6. hunter2 says:

    wow, you just poorly copied the code from a C# tutorial here http://www.csharp-station.com/HowTo/ProcessStart.aspx

    I wish I could be as cool as you

    • OmegaMan says:

      Two things

      1) Yes Joe Mayo’s post is much more detailed and the commonality besides the process is the use of Notepad to launch. While his was meant as a detailed learning experience written in the heady days of .Net 1 my article was written as a quick lookup example for MSDN forum posts years later. Notepad was used because everyone has notepad and launching that seems; well obvious. I didn’t even know Joe’s article existed until now actually and frankly except for the topic and use of Notepad, there is very little similarity (yes his is better).

      2) I am actually friends with Joe Mayo. He lives here in Denver and we both helpout/speak for the Denver Visual Studio’s User Group and as mentioned do I consider him a friend. Don’t believe me? Check out Joe’s tweet when he was going to see me speak on Silverlight 5:

      Heading to Denver VS.NET User Group tonight to see @OmegaMan present Silverlight 5

      So to sum up, I did not plagiarize Joe Mayo’s work; nor never have. Similarities on thier own do not lend themselves to a proof. My blog regularly gets plagiarized verbatim as I am sure Joe’s writings do too and that is a problem on the net…. But I do not need to copy someone else’s work; I may leverage it in writing a new article, but since my days as an editor on my school paper (I was thinking about a journalism major at one point before deciding on CS) I take plagiarism and libel very seriously and would never engage in that activity.

      PS: As to being cool, well cool is as cool does….

  7. Joe Mayo says:

    I’m sure any similarity is coincidental – this is fine. Thanks for the concern.

    Joe

Leave a Reply to Joe Mayo