Using Diagnostics.Process to start an external application.
This is a beginner-level article to show how to start an external application from C#.
Introduction
A very basic thing that is usually required for programmers, is to start an external process from your application. This may be a simple call to Notepad.exe to let the user view your Readme.txt file, or it may be some complex project.
I recently had to use these facilities to install a Windows service. This complicated matters, since I also needed the environment variable required for the Windows Path.
After a bit of searching in the .NET libraries, I found that I needed no Windows API call to CreateProcess
like I used to do when I was using C++.
The .NET framework includes a class called Process
, and it is included in the Diagnostics
namespace.
All you need to do is to include the namespace, using System.Diagnostics;
, and at the place where you wish to start the application, call the Process.Start
method, like so:
Process.Start("notepad", "readme.txt");
I wanted to create a service, and the executable path could not include a string like "%windir%";
, so I had a long search before I discovered the Environment
class in the .NET framework. To install my service, I could then call the Installutil application that is included in the .NET folder, like so:
string winpath = Environment.GetEnvironmentVariable("windir");
string path = System.IO.Path.GetDirectoryName(
System.Windows.Forms.Application.ExecutablePath);
Process.Start(winpath + @"\Microsoft.NET\Framework\v1.0.3705\Installutil.exe",
path + "\\MyService.exe");
The above illustrates how the Diagnostics.Process
class eases the task of creating a process. The Start
method returns a process, so you can inspect its properties after it started.
Enjoy!