Click here to Skip to main content
Licence CPOL
First Posted 4 Jun 2003
Views 134,201
Bookmarked 40 times

Using Diagnostics.Process to start an external application.

By | 4 Jun 2003 | Article
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!

License

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

About the Author

WillemSe

Software Developer (Senior)
DigiCore Technology
South Africa South Africa

Member

Software developer

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionProcess.Start() ... NOT STARTED ??? PinmemberEkvilibrium7:06 16 Jan '10  
GeneralMy vote of 2 Pinmembertitwan5:12 3 Dec '08  
Generaldoc 2 pdf PinmemberAli Ghazaei10:13 6 Oct '08  
GeneralShell to use PDF printer PinmemberAli Ghazaei13:06 28 Sep '08  
Questionhow to use parameter PinmemberMember 372990220:06 28 Aug '08  
GeneralDifferent article/good job PinmemberSamuel Allen13:37 7 Jun '08  
GeneralMy external App is not finishing Pinmemberliorjj22:22 20 Oct '07  
QuestionHow to start an application in foreground PinmemberAbdul Hasib1:18 2 Jul '07  
AnswerRe: How to start an application in foreground Pinmembernekropatolog21:36 26 Aug '07  
QuestionProcess.Start("Notepad.exe") from Service. PinmemberKent Keller13:05 3 Apr '07  
QuestionRe: Process.Start("Notepad.exe") from Service. Pinmemberauxcom15:14 25 May '08  
I also need this functionality for my Windows Service, to execute external application. Do you already have solution for this?
 
Daniel
QuestionTo call a program PinmemberalexTCT14:52 17 Oct '06  
QuestionHow to use runas ? PinmemberRedaemon6:24 20 Jan '06  
Questionan funny problem Pinmembermostafa.hk18:25 13 Jan '06  
QuestionWhat about starting a process as a different user? Pinmemberwyatt blake1:14 4 Oct '05  
GeneralBackground UI PinmemberZenlander20:34 30 Jun '05  
GeneralRe: Background UI PinmemberITGFanatic12:00 6 Mar '07  
GeneralRunning external application in .Net CF PinsussVasil Zhekov4:16 13 Oct '04  
GeneralRe: Running external application in .Net CF PinmemberCRoberto2:32 4 Feb '05  
GeneralRunning the process inside the application PinmemberMesrop Simonian11:47 27 Feb '04  
GeneralAbsoute paths PinmemberNameOfTheDragon0:38 11 Jun '03  
GeneralRe: Absoute paths PinmemberWillemSe6:58 11 Jun '03  
GeneralRe: Absoute paths Pinmember94stangfan12:22 16 Aug '04  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 5 Jun 2003
Article Copyright 2003 by WillemSe
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid