Executing external applications using Process class
Introduction
For you to follow this tutorial, you will need to have installed on your computer
Visual Studio .NET and .NET Framework. If you only want to run the application,
then the only thing you will need if .NET Framework
Getting to Work
Open Visual
Studio .NET (if you haven't done this yet) and create a new
C# Windows Application (choose File -> New -> Project or press Ctrl
+ Shift + N). From here, choose Visual C# Projects from the left pane and select
Windows Application from the right pane. Name it however you like and choose
a path where the project will be created. Now press OK.
If everything went well,
you are now in front of the Design view. Here, we
will need to create first the interface for our application, after witch we
will add functionality to it. First, drag and drop from the Toolbox a Label control and set its Text to "Application to Execute:" (if the Toolbox is not visible, select View
-> Toolbox or press Ctrl + Alt +X ). Now add
a Textbox control next to that Label. Set its Name to tb_app and clear its
Text. Next, add a button named btn_browse and with the Text set to "Browse...".
We will use this Button to select an application, rather then typing the path
to it. Let's add another Label with the Text property set to "Optional
Parameters:" and a Textbox with the Name tb_params. Next, we add another
Label and set its Text to "Execute After:" and a NumericUpDown control. Add another Label with the Text "minutes" after the NumericUpDown control and a Button with the Name set to btn_go and Text "GO!!!".
The last thing we need to add is a Timer control, with the Interval set to
60000 milliseconds. This is the end of the design part of the application.
Here is how my application looks like after completing the steps above:
If yours looks different, don't worry!
Now it's time to get down to coding. Before we begin creating any methods,
we must include System.Diagnostics namespace(using System.Diagnostics), witch
contains the Process class. Also, we must declare a private variable of type
Process in our class. Let's name the variable p.
Let's go to Design view
and double-click the Browse... button. Visual
Studio .NET will create a method called btn_browse_Click.
In this method, we will need to add code to select a file from the user's hard
disk. To
accomplish this, we will use an OpenFileDialog control. Bellow
is the code that needs to go into this method:
|
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
tb_app.Text = dlg.FileName;
}
|
What we did is created an instance of the OpenFileDialog class and called
its ShowDialog method. This displays the control and lets the user choose a
file. If, after selecting a file, the user presses OK button, we put the file
name into the tb_app Textbox. We are about finished with this method.
Next, let's go to the Design view again and double-click the "GO!!!" button.
In the new method, we will add code to start the application.
|
timer1.Start();
p = new Process();
p.StartInfo.FileName = tb_app.Text.Trim();
p.StartInfo.Arguments=tb_params.Text;
|
The first thing we need to do here is start the timer. Once started, at the
specified interval (60000 milliseconds), the timer will execute a method that
we will create later. For the moment, we only start it. Next, we will instantiate
the Process class and we set the file name to execute to the file that we choose
using the Browse button. Sometimes, an application needs to have some parameters
set. That's why we created the second Textbox, to fill it with the parameters
required(if no parameters are required, we leave it blank).
|
if ((int)numericUpDown1.Value == 0)
{
try
{
p.Start();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
timer1.Stop();
}
else
numericUpDown1.Enabled = false;
|
Now, if the numericUpDown1 control
has the value of 0, we execute the process right away. The try-catch block
is required because the user can type something
like "bla bla" and our application will raise an exception that
will remain unhandled. By using the try-catch, we just display a friendly message
and we continue the application. After executing the desired program, we will
need to stop the timer. Finally, if the numericUpDown1 value is not 0, we only
disable the numericUpDown1 control, so that the user cannot modify the interval
once he presses GO button.
The last method we will need to handle is the timer1_Tick method, created by
double-clicking the timer1 control in the Design view. Before we begin to code
this method, we will need to add a variable called minutes of type int and
initialize it with 0. This variable will be out counter for the minutes that
passed since the user pressed GO button.
|
if (minutes == (int)numericUpDown1.Value)
{
try
{
p.Start();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
timer1.Stop();
numericUpDown1.Enabled = true;
minutes = 0;
}
else
minutes++;
|
If our minutes variable equals the number of minutes entered in the numericUpDown1 control, we will execute the program in the tb_app Textbox. After exiting the
program, we will stop the timer, enable the NumericUpDown control and reset
the minutes that have elapsed. Else, we increment minutes.
You are probably wondering if we really need parameters at all. Why not type
the parameters after the program's name in the tb_app Textbox? Here is
an example of running a program with/without using parameters. In the tb_app Textbox, type this: "cmd /c notepad.exe". This command should open
the console, execute notepad, waits for the user to quit notepad and closes
the console. Try running it by pressing GO!!!. You will get an error saying "The
system cannot find the file specified". Now, type "cmd" in
the tb_app Textbox and "/c notepad.exe" in the tb_params Textbox.
Try running it. It works, doesn't it?
Take a look at the application in action on my computer:

This is about it. Until next time, good luck and happy programming!
Raul POPESCU
Attachments:
Project Files: ExecApps.zip