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.

| How do you implement Observer Design Pattern in .NET? | Introduction to C# (C Sharp)|Debugging in ASP.NET| How do you pass data between different Tiers in .NET Architecture? | How is Classic ADO different from ADO.NET? | How is Dataadapter useful in ADO.NET? | How is Datareader different from Dataset in ADO.NET? | How is .NET Application Development different from Traditional Development? | How is HashTable different from ArrayList in .NET? | How is Inheritance achieved in C#? | How is new keyword different from override keyword during inheritance in .NET? | How is String class different from StringBuilder class in .NET? | Illustrate ADO.NET Architecture | Illustrate the importance of Server.Transfer and Response.Redirect in .NET? | Mention the different objects available in Dataset of ADO.NET | Mention the usage of Connection Object in ADO.NET | What are the commonly used methods of Dataadapter in ADO.NET? | What are the different Behavioral Design Patterns that can be used in .NET Architecture? | What are the different Creational Design Patterns that can be used in .NET Architecture? | What are the different Structural Design Patterns that can be used in .NET Architecture? | What are the methods provided by Command Objects in ADO.NET? | What is Internal Access Modifier in C#? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

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

Copyright - © 2004 - 2024 - All Rights Reserved.