Calling an External Process from Windows

Calling an External Process from Windows

You might have developed many projects and want to deploy them to the clients but want to avoid having number of entry point for the application. You can build a master application which can invoke all these small tools to ensure the entry point is hooked to one single executable. Here is a solution to it.

Lets say you have three executables that could run independently. And you want to combine them together into a single application, such that, when x.exe is called, the application X will be launched.

To launch an application from a .NET code, please use the following code:

string path = Path.Combine(Assembly.GetExecutingAssembly().Location, "x.exe");
using (Process p = new Process())
{
p.StartInfo = new ProcessStartInfo(path, "");
p.Start();
}

The above code will invoke x.exe from the current directory from where the executable has started. If you place this code in the click of a Button of a Form, it will invoke the external application. You can get hold of the object p, inside your program (you need to ensure it is not used inside the Disposable using block) and kill the process whenever needed.

p.Kill()

The above code kills the process.

Related Tip : How to get list of attached processes in Visual Studio while debugging ?

You can also use the static start method to invoke the same thing.

System.Diagonistics.Process.Start(path);

Pass arguments to an external process:

The process sometimes needs arguments to launch. The Process API supports you to pass arguments to it such that it can use it when the process gets launched. The arguments are used to pass data to the external process so that the process can customize itself accordingly. Take an instance, say a process is used to show a photo in a Photoviewer. When the process gets lauched you can pass in the file path, such that when the process gets open, the PhotoViewer shows the correct image.

string path = Path.Combine(Assembly.GetExecutingAssembly().Location, "x.exe");
using (Process p = new Process())
{
p.StartInfo = new ProcessStartInfo(path, "10");
p.Start();
}

Here when the process gets launched, it gets an argument 10 inside it. If you are writing the code for x.exe as well, the process will get 10 as argument in the Main method in Program.cs file.

static void Main(string[] args)
{
Application.Run(new Form1());
}

Here in the above Main method, the program finds an array of all the arguments passed to it when it gets launched.

Similarly, if you want to pass multiple arguments to the process you can also use the following code to do that.

string[] args = { "10", "20", "\"This is test argument\""};

string path = Path.Combine(Assembly.GetExecutingAssembly().Location, "x.exe");
using (Process p = new Process())
{
p.StartInfo = new ProcessStartInfo(path, String.Join(" ", args));
p.Start();
}

In the above code, the process x.exe is launched with three arguments. The first two are 10 and 20. The third one as it have space inside the string, you need to pass an extra quotation mark inside the string as argument.

Similar to launching a process, you can also get a list of all the processes using :

Process.GetProcesses();
Process.GetProcesses
List of all processes running in current machine

or you can have a reference to a process using Process.GetProcessById(processid) where you need to pass the Processid as reference.

You can also call an external process or a Windows process or an application installed in windows. The default path of the Windows belongs to System32 folder under Windows directory. To call a process you can use only the executable name and you are done. For instance,

Process.Start("IExplore.exe", "www.dailydotnettips.com")

The above code will open Internet explorer and start pointing to the current site.

Hope this post will come handy.

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at http://www.abhisheksur.com His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434

One Comment to “Calling an External Process from Windows”

Comments are closed.