How to Start and kill a process (exe) From dot net

Hello friends,
First of all sorry for such a late post bit busy in some other stuff but come back with bang.
Today I will show you some simple but very interesting topic. So first topic is how to call a process from your code. And how to kill a particular process from your code.
Its code is very simple just needs to understand once then you are perfect in this.
The first thing you need to know there is base class for process which is System.Diagnostics
I am showing you code right here so please with me for few moments.
I am opening a Microsoft word application here.

Imports System
Imports System.Diagnostics
Imports System.Diagnostics.Process

Private Sub btnStartProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartProcess.Click
Try
Dim oProcess As Process
Dim startInfo As New ProcessStartInfo
startInfo.FileName = “winword.exe”
oProcess = Process.Start(startInfo)
Catch ex As Exception
Throw ex
End Try
End Sub

In this way you can open a new Microsoft word application in similar manner you can open different other process. And also pass. Command line arguments to them.

Now if you want to close this application then its also easy. Just see below code and you will be master in that.

Private Sub btncloseProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncloseProcess.Click
Try
Dim oProcess As Process
Process.GetProcessesByName(“winword.exe”)
oProcess.Kill()
Catch ex As Exception
Throw ex
End Try
End Sub

If you know the id of process then it could be more specific. To kill particular instance.
In this way you can create and kill a process.
In next session I will come up with some more interesting topics till than do your best enjoy programming, enjoy dot net.
Thanks & regards
Rajat

Advertisements

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