jump to navigation

Using the Process Component to launch applications from our .Net application December 3, 2008

Posted by fofo in .NET, VB 2005, VB 9.0, Visual Studio 2005, Visual Studio 2008.
Tags: , , ,

In this post I will show with a simple example how to use the Process control to launch applications from a windows form application (vb version), with this control that was shipped with the .Net 2.0 version you can launch very easily all sort of applications.

Let’s get started then…

1) Launch Visual Studio 2008/2005-express editions will do

2) Create a windows form application. Choose VB as your development language.

3) Name your solution project with a name of your choice

4) Add 3 buttons on the form. Change the Text properties of the 3 button to “Launch Notepad”, “Launch Wordpad”, “Launch MS Paint”

5) Drag and Drop 3 Process controls from the ToolBox (Under  the Component tab) and name them “NotePadProcess”, “WordPadProcess”,”MSPaintProcess”

6) Add a reference to your project to the System.IO namespace from the Solutions explorer window, by right-clicking on your project name, then Properties, then Reference and select System.IO from the imported namespaces

7) Double Click on the button that is destined to launch Notepad. In the body of the routine type

mypath = System.Environment.GetFolderPath(Environment.SpecialFolder.System)
mypath = Path.Combine(mypath, “notepad.exe”)
NotepadProcess.StartInfo.FileName = mypath

the code is pretty straight forward.We get to the notepad.exe file by finding the path and then we start notepad

8) Run your application, click on the “Notepad” button and see notepad launching

9) Double click on the two other buttons on the form designer. In their respective event handling routines type

for the button the launches wordpad

Dim mypath As String
mypath = System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
mypath = Path.Combine(mypath, “Windows NT\Accessories\wordpad.exe”)
WordPadProcess.StartInfo.FileName = mypath

for the button the launches paint

Dim mypath As String
mypath = System.Environment.GetFolderPath(Environment.SpecialFolder.System)
mypath = Path.Combine(mypath, “mspaint.exe”)
MSPaintProcess.StartInfo.FileName = mypath

10) Run your application and then click on all the buttons

11) If you need to close all these applications programmatically you can add another 3 buttons on the form and (names:CloseNotepad,CloseWordpad,CloseMsPaint).Double click on the buttons and type respectively


12) Run your application. In order for this example to work, click once on the buttons to launch all 3 applications and then click on the “close” buttons to close the applications

13) If you try to close “Notepad” without first launching notepad you will receive an error.If you want to avoid that and close all the notepad instances you opened

instead of


type in the body of the event handling routine

Dim npProc() As Process
npProc = Process.GetProcesses
For Each proc As Process In npProc
If proc.ProcessName.Equals(“notepad”) Then
End If
Catch ex As Exception
End Try

14) Do the same for the remaing two event handling routines that close MsPaint,WordPad

15) Now if you want to capture the moment that notepad closes, for any reason you need to add this line of  code

NotepadProcess.EnableRaisingEvents = True

in the button click event that launches Notepad.

So the complete code for the button that launches Notepad is

Dim mypath As String
mypath = System.Environment.GetFolderPath(Environment.SpecialFolder.System)
mypath = Path.Combine(mypath, “notepad.exe”)
NotepadProcess.EnableRaisingEvents = True
NotepadProcess.StartInfo.FileName = mypath

16)  Find the NotepadProcess (if you called it like that) process and choose the event Exited from the code window. The complete code is this

Private Sub NotepadProcess_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotepadProcess.Exited
MessageBox.Show(“Notepad Exited”)
End Sub

17) Run your application. Launch Notepad. If you close Notepad by either clicking on the respective button on your form or just close Notepad from the close button, the code within this event will run

That is all folks!!!!

If you need the source code email me or leave a message.

Add to FacebookAdd to NewsvineAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to Ma.gnoliaAdd to TechnoratiAdd to Furl


1. Sean - October 5, 2009

How should be declare the NotepadProcess variable?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: