Click here to Skip to main content
15,881,709 members
Articles / Programming Languages / C#
Article

Using Diagnostics.Process to start an external application.

Rate me:
Please Sign up or sign in to vote.
3.35/5 (39 votes)
4 Jun 2003CPOL1 min read 294.2K   46   24
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:

C#
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:

C#
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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) DigiCore Technology
South Africa South Africa
Software developer

Comments and Discussions

 
QuestionStart external process Pin
naveen ranjan21-Mar-14 22:10
naveen ranjan21-Mar-14 22:10 
QuestionProcess.Start() ... NOT STARTED ??? Pin
Ekvilibrium16-Jan-10 7:06
Ekvilibrium16-Jan-10 7:06 
Q: In my asp.net application i am using class Process from System.Diagnostic...
For example: After client click on web page, on server side starting method Process.Start("applic.exe", arg), BUT applic.exe NOT YET Starting Frown | :(
I trying start it from command-line (cmd) with arg... all working fine...
May be problem with security with IUSR_COMP account or w3wp process ?

Thanks for advice...
GeneralMy vote of 2 Pin
titwan3-Dec-08 5:12
professionaltitwan3-Dec-08 5:12 
Generaldoc 2 pdf Pin
Ali Ghazaei6-Oct-08 10:13
Ali Ghazaei6-Oct-08 10:13 
GeneralShell to use PDF printer Pin
Ali Ghazaei28-Sep-08 13:06
Ali Ghazaei28-Sep-08 13:06 
Questionhow to use parameter Pin
KashyapPatel28-Aug-08 20:06
KashyapPatel28-Aug-08 20:06 
GeneralDifferent article/good job Pin
Samuel Allen7-Jun-08 13:37
Samuel Allen7-Jun-08 13:37 
GeneralMy external App is not finishing Pin
liorjj20-Oct-07 22:22
liorjj20-Oct-07 22:22 
QuestionHow to start an application in foreground Pin
Abdul Hasib2-Jul-07 1:18
Abdul Hasib2-Jul-07 1:18 
AnswerRe: How to start an application in foreground Pin
nekropatolog26-Aug-07 21:36
nekropatolog26-Aug-07 21:36 
QuestionProcess.Start("Notepad.exe") from Service. Pin
Kent Keller3-Apr-07 13:05
Kent Keller3-Apr-07 13:05 
QuestionRe: Process.Start("Notepad.exe") from Service. Pin
auxcom25-May-08 15:14
auxcom25-May-08 15:14 
QuestionTo call a program Pin
alexTCT17-Oct-06 14:52
alexTCT17-Oct-06 14:52 
QuestionHow to use runas ? Pin
Ariston Darmayuda20-Jan-06 6:24
Ariston Darmayuda20-Jan-06 6:24 
Questionan funny problem Pin
mostafa.hk13-Jan-06 18:25
mostafa.hk13-Jan-06 18:25 
QuestionWhat about starting a process as a different user? Pin
wyatt blake4-Oct-05 1:14
wyatt blake4-Oct-05 1:14 
GeneralBackground UI Pin
Zenlander30-Jun-05 20:34
Zenlander30-Jun-05 20:34 
GeneralRe: Background UI Pin
ITGFanatic6-Mar-07 12:00
ITGFanatic6-Mar-07 12:00 
GeneralRunning external application in .Net CF Pin
Zhekov13-Oct-04 4:16
Zhekov13-Oct-04 4:16 
GeneralRe: Running external application in .Net CF Pin
CRoberto4-Feb-05 2:32
CRoberto4-Feb-05 2:32 
GeneralRunning the process inside the application Pin
Mesrop Simonian27-Feb-04 11:47
Mesrop Simonian27-Feb-04 11:47 
GeneralAbsoute paths Pin
NameOfTheDragon11-Jun-03 0:38
NameOfTheDragon11-Jun-03 0:38 
GeneralRe: Absoute paths Pin
WillemSe11-Jun-03 6:58
WillemSe11-Jun-03 6:58 
GeneralRe: Absoute paths Pin
94stangfan16-Aug-04 12:22
94stangfan16-Aug-04 12:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.