How to call other external applications from your .Net application?

Calling an external application from your .Net application is frequently done if you are developing a product. During the installation of a product you might require to redirect the users who are installing your product to some web pages of your web site where they may get some information regarding your product. This web page might be a user manual in html format. You may sometimes be required to open a readme file in text format. This file is usually opened using a Notepad application. So there are many such requirements which require you to call an external application from your .Net application.


We will see some simple examples on how to call and execute an external application. For this we will create a console application with the following code:

Module Module1
Sub Main()
System.Diagnostics.Process.Start(“winword.exe”)
End Sub
End Module

The above code is written in VB.Net to call the Microsoft Word application from your .Net console application. Once your application is run, it opens the Word external application and your application terminates. Suppose you want to open an existing word document using your .Net console application you can pass the filename of the work document as an argument to the Start method of the Process class. This can be achieved as given below:

Module Module1
Sub Main()
System.Diagnostics.Process.Start(“winword.exe”, _ “first.doc”)
End Sub
End Module

For the above document to be opened properly you should have the document “first.doc” in the folder which contains the exe file of your application. Or else you have to specify the path of the document properly. You can also specify the path to avoid such problems in locating the file as,

System.Diagnostics.Process.Start(“winword.exe”, “c:\first.doc”)

Your application terminates after opening the document. Suppose you want to make your application wait for sometime while you work in the document, you can make it wait by using the WaitForExit() method of the process. A small change in your code would make your application wait indefinitely.

Dim p As New System.Diagnostics.Process()
p.Start("winword.exe", "c:\first.doc")
p.WaitForExit(10000)

The above code should make your application wait for 10 secs before it terminates. If you don’t pass any arguments to the WaitForExit() method, it will make your application wait indefinitely.

It is possible to set many other process information for the process that opens the external application. For example for the above process which opens the MS Word application you can also set other process information as given in the code below:

Module Module1

Sub Main()

Dim pi As New System.Diagnostics.ProcessStartInfo()
pi.FileName = "winword.exe"
pi.Arguments = "c:\first.doc"
pi.WorkingDirectory = "c:\"
pi.WindowStyle = ProcessWindowStyle.Maximized
Dim p As New System.Diagnostics.Process()
p.Start(pi)

End Sub

End Module

You can set the process start information properties such as Arguments, WorkingDirectory, and WindowStyle using the above code. There are other properties that might be useful to you. The Arguments property takes the filename that is to be opened, and the FileName properties takes the application name that is to be opened. You can set the working directory using the WorkingDirectory property. The window style can also be set using the WindowStyle property.

In some case you might be required to redirect the users to some web page in the internet. To achieve this you can just pass the URL of the web page to the Start method of the process as given below:

System.Diagnostics.Process.Start(“www.yoursite.com”)

The above code opens the default browser in your system to open the website given in the URL that is passed to the Start method. Thus you find it is easy to call an external application from your .Net application.

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.