5,667,575 members and growing! (9,185 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, .NET, Visual Studio, Dev

Posted: 4 Jun 2003
Updated: 4 Jun 2003
Views: 94,345
Bookmarked: 33 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
34 votes for this Article.
Popularity: 4.65 Rating: 3.04 out of 5
14 votes, 41.2%
1
1 vote, 2.9%
2
0 votes, 0.0%
3
3 votes, 8.8%
4
16 votes, 47.1%
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


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

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
Generaldoc 2 pdfmemberAli Ghazaei11:13 6 Oct '08  
GeneralShell to use PDF printermemberAli Ghazaei14:06 28 Sep '08  
Questionhow to use parametermemberMember 372990221:06 28 Aug '08  
GeneralDifferent article/good jobmemberSamuel Allen14:37 7 Jun '08  
GeneralMy external App is not finishingmemberliorjj23:22 20 Oct '07  
GeneralHow to start an application in foregroundmemberAbdul Hasib2:18 2 Jul '07  
AnswerRe: How to start an application in foregroundmembernekropatolog22:36 26 Aug '07  
QuestionProcess.Start("Notepad.exe") from Service.memberKent Keller14:05 3 Apr '07  
QuestionRe: Process.Start("Notepad.exe") from Service.memberauxcom16:14 25 May '08  
QuestionTo call a programmemberalexTCT15:52 17 Oct '06  
QuestionHow to use runas ?memberRedaemon7:24 20 Jan '06  
Questionan funny problemmembermostafa.hk19:25 13 Jan '06  
GeneralWhat about starting a process as a different user?memberwyatt blake2:14 4 Oct '05  
GeneralBackground UImemberZenlander21:34 30 Jun '05  
GeneralRe: Background UImemberITGFanatic13:00 6 Mar '07  
GeneralRunning external application in .Net CFsussVasil Zhekov5:16 13 Oct '04  
GeneralRe: Running external application in .Net CFmemberCRoberto3:32 4 Feb '05  
GeneralRunning the process inside the applicationmemberMesrop Simonian12:47 27 Feb '04  
GeneralAbsoute pathsmemberNameOfTheDragon1:38 11 Jun '03  
GeneralRe: Absoute pathsmemberWillemSe7:58 11 Jun '03  
GeneralRe: Absoute pathsmember94stangfan13: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-2008
Web12 | Advertise on the Code Project