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.
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);
}
You are right! In my defense, this article was meant for demonstration purposes and was not optimized.
Great knowledge! Thanks for sharing this, it’s saved my time.
Nice snippet! Including “using” statements would also be very helpful :)
It might be useful to know that Process is contained in System.Diagnostics
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
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:
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….
The link to the original article changed to https://csharp-station.com/Article/Index/ProcessStart
I’m sure any similarity is coincidental – this is fine. Thanks for the concern.
Joe