Click here to Skip to main content
6,306,412 members and growing! (18,475 online)
Email Password   helpLost your password?
Languages » C# » How To     Intermediate License: The Code Project Open License (CPOL)

Using Diagnostics.Process to start an external application.

By WillemSe

This is a beginner-level article to show how to start an external application from C#.
C#, Windows, .NET 1.0, Visual Studio, Dev
Posted:4 Jun 2003
Views:103,218
Bookmarked:37 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
35 votes for this article.
Popularity: 4.63 Rating: 3.00 out of 5
14 votes, 40.0%
1
2 votes, 5.7%
2

3
3 votes, 8.6%
4
16 votes, 45.7%
5

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


Member
Software developer
Occupation: Software Developer (Senior)
Company: DigiCore Technology
Location: South Africa South Africa

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
GeneralMy vote of 2 Pinmembertitwan6:12 3 Dec '08  
Generaldoc 2 pdf PinmemberAli Ghazaei11:13 6 Oct '08  
GeneralShell to use PDF printer PinmemberAli Ghazaei14:06 28 Sep '08  
Questionhow to use parameter PinmemberMember 372990221:06 28 Aug '08  
GeneralDifferent article/good job PinmemberSamuel Allen14:37 7 Jun '08  
GeneralMy external App is not finishing Pinmemberliorjj23:22 20 Oct '07  
GeneralHow to start an application in foreground PinmemberAbdul Hasib2:18 2 Jul '07  
AnswerRe: How to start an application in foreground Pinmembernekropatolog22:36 26 Aug '07  
QuestionProcess.Start("Notepad.exe") from Service. PinmemberKent Keller14:05 3 Apr '07  
QuestionRe: Process.Start("Notepad.exe") from Service. Pinmemberauxcom16:14 25 May '08  
QuestionTo call a program PinmemberalexTCT15:52 17 Oct '06  
QuestionHow to use runas ? PinmemberRedaemon7:24 20 Jan '06  
Questionan funny problem Pinmembermostafa.hk19:25 13 Jan '06  
GeneralWhat about starting a process as a different user? Pinmemberwyatt blake2:14 4 Oct '05  
GeneralBackground UI PinmemberZenlander21:34 30 Jun '05  
GeneralRe: Background UI PinmemberITGFanatic13:00 6 Mar '07  
GeneralRunning external application in .Net CF PinsussVasil Zhekov5:16 13 Oct '04  
GeneralRe: Running external application in .Net CF PinmemberCRoberto3:32 4 Feb '05  
GeneralRunning the process inside the application PinmemberMesrop Simonian12:47 27 Feb '04  
GeneralAbsoute paths PinmemberNameOfTheDragon1:38 11 Jun '03  
GeneralRe: Absoute paths PinmemberWillemSe7:58 11 Jun '03  
GeneralRe: Absoute paths Pinmember94stangfan13:22 16 Aug '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Jun 2003
Editor: Smitha Vijayan
Copyright 2003 by WillemSe
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project