Click here to Skip to main content
Click here to Skip to main content

Using Diagnostics.Process to start an external application.

, 4 Jun 2003
Rate this:
Please Sign up or sign in to vote.
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
Software developer

Comments and Discussions

 
QuestionStart external process Pinmembernaveen ranjan21-Mar-14 22:10 
QuestionProcess.Start() ... NOT STARTED ??? PinmemberEkvilibrium16-Jan-10 7:06 
GeneralMy vote of 2 Pinmembertitwan3-Dec-08 5:12 
Generaldoc 2 pdf PinmemberAli Ghazaei6-Oct-08 10:13 
GeneralShell to use PDF printer PinmemberAli Ghazaei28-Sep-08 13:06 
Questionhow to use parameter PinmemberMember 372990228-Aug-08 20:06 
GeneralDifferent article/good job PinmemberSamuel Allen7-Jun-08 13:37 
GeneralMy external App is not finishing Pinmemberliorjj20-Oct-07 22:22 
QuestionHow to start an application in foreground PinmemberAbdul Hasib2-Jul-07 1:18 
AnswerRe: How to start an application in foreground Pinmembernekropatolog26-Aug-07 21:36 
QuestionProcess.Start("Notepad.exe") from Service. PinmemberKent Keller3-Apr-07 13:05 
QuestionRe: Process.Start("Notepad.exe") from Service. Pinmemberauxcom25-May-08 15:14 
QuestionTo call a program PinmemberalexTCT17-Oct-06 14:52 
QuestionHow to use runas ? PinmemberRedaemon20-Jan-06 6:24 
Questionan funny problem Pinmembermostafa.hk13-Jan-06 18:25 
QuestionWhat about starting a process as a different user? Pinmemberwyatt blake4-Oct-05 1:14 
GeneralBackground UI PinmemberZenlander30-Jun-05 20:34 
GeneralRe: Background UI PinmemberITGFanatic6-Mar-07 12:00 
GeneralRunning external application in .Net CF PinsussVasil Zhekov13-Oct-04 4:16 
GeneralRe: Running external application in .Net CF PinmemberCRoberto4-Feb-05 2:32 
GeneralRunning the process inside the application PinmemberMesrop Simonian27-Feb-04 11:47 
GeneralAbsoute paths PinmemberNameOfTheDragon11-Jun-03 0:38 
GeneralRe: Absoute paths PinmemberWillemSe11-Jun-03 6:58 
GeneralRe: Absoute paths Pinmember94stangfan16-Aug-04 12:22 

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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 5 Jun 2003
Article Copyright 2003 by WillemSe
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid