How can I run another application or batch file from my Visual C# .NET code?

Published 01 June 04 11:54 PM

Posted by: Duncan Mackenzie, MSDN
This post applies to Visual C# .NET 2002/2003

Suppose you want to run a command line application, open up another Windows program, or even bring up the default web browser or email program... how can you do this from your C# code?

The answer for all of these examples is the same, you can use the classes and methods in System.Diagnostics.Process to accomplish these tasks and more.

Example 1. Running a command line application, without concern for the results:

private void simpleRun_Click(object sender, System.EventArgs e){
 System.Diagnostics.Process.Start(@"C:\listfiles.bat");
}

Example 2. Retrieving the results and waiting until the process stops (running the process synchronously):

private void runSyncAndGetResults_Click(object sender, System.EventArgs e){
 System.Diagnostics.ProcessStartInfo psi =
  
new System.Diagnostics.ProcessStartInfo(@"C:\listfiles.bat");
 psi.RedirectStandardOutput =
true;
 psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
 psi.UseShellExecute =
false;
 System.Diagnostics.Process listFiles;
 listFiles = System.Diagnostics.Process.Start(psi);
 System.IO.StreamReader myOutput = listFiles.StandardOutput;
 listFiles.WaitForExit(2000);
 
if (listFiles.HasExited)
 {
  
string output = myOutput.ReadToEnd();
  
this.processResults.Text = output;
 }
}

 Example 3. Displaying a URL using the default browser on the user's machine:

private void launchURL_Click(object sender, System.EventArgs e){
 
string targetURL = @http://www.duncanmackenzie.net;
 System.Diagnostics.Process.Start(targetURL);
}

In my opinion, you are much better off following the third example for URLs, as opposed to executing IE with the URL as an argument. The code shown for Example 3 will launch the user's default browser, which may or may not be IE; you are more likely to provide the user with the experience they want and you will be taking advantage of the browser that is most likely to have up-to-date connection information.

C# code download available from http://www.duncanmackenzie.net/Samples/default.aspx

By the way, this post is a simple port of an earlier VB FAQ post... just in case you thought you had seen it already in your feeds...

Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Ryan Farley said on June 2, 2004 9:44 AM:
"you are much better off following the third example for URLs, as opposed to executing IE with the URL as an argument"

I don't agree. While shelling the URL directly will cause it to launch in the default browser, it will also "take over" any already open browser window - instead of causing it to open a new one. I hate it when a URL is launched from an app as described in example 3 and it navigates me away from some already opened page I was reading in my browser.

I am not saying you should force IE either. If you read the default browser from the registry (app associated with HTTP) and use that to open a URL on the command-line to it then you get the best of both worlds. I've outlined this here:
http://ryanfarley.com/blog/archive/2004/05/16/649.aspx

-Ryan
# Duncan Mackenzie said on June 2, 2004 10:23 AM:
Ryan, the behaviour you describe occurs because IE is set to "reuse open browser windows", which is exactly what it does.

If a user doesn't want IE to navigate away from a currently open page, they should change that setting inside IE (which I know you think is silly... but that is what that setting is for). Why would they want other applications to reuse, but not yours? Personally, I hate the reuse of browser windows... but I hate it no matter where I clicked on the URL... I don't care if it is from within SharpReader, Outlook, or the Run option on the Start menu... if reuse annoys me, it annoys me globally so I should turn off that setting.

Shelling a process is a full-trust operation at the moment, so accessing the registry isn't really a security issue but I still do not suggest that method of launching a URL... you are essentially overriding the user's preferences which they can control through IE's settings.

Even though we disagree, I did find your article to be quite well-written and I'm sure you aren't alone in wanting to get around these settings though. Having a link to your post/sample in these comments will be useful to folks reading this post.
# Ryan Farley said on June 2, 2004 11:01 AM:
Duncan, those are good points. I agree that typically if the setting is an annoyance at all then it is likely an annoyance globally so why would you not just turn it off? If it is just me, then fine - I do turn that setting off. But telling a system admin or IT team that this is what they have to do for all 2000+ users using your app doesn't always fly well. But, like you said - that is what the setting is for. It is just that complaining end-users do not always agree, so it is good to be able to code a way around it.

Something I love about Outlook 2003 is that it will *always* launch a link in a new browser window - regardless of the setting to reuse windows in IE. I don't feel like Outlook is overriding my preferences, just making a good decision to responsibly open the link in a new window.

Anyway, thanks for the post. This might seem like a simple topic, but I get asked this *every* time I am doing developer training. So it is good to have a post here I can refer them to.
# Shawn said on June 2, 2004 5:59 PM:
Interesting bit of blog synergy here, I just posted an entry today about the new Whidbey features of the Process class that lets you start a new process under the context of a different user: http://blogs.msdn.com/shawnfa/archive/2004/06/02/146915.aspx
# James Mc Kenzie said on June 29, 2004 9:28 AM:
Under what user context does this application start under?, What permissions would it have?
# Duncan Mackenzie said on June 29, 2004 9:32 AM:
It runs under the same user context as the application that started it, so... in the case of a Windows Forms application it would be under the currently logged on user's context... in the case of an ASP.NET application it would be in the context of whatever user you are running your web pages under (yourcomputer\ASPNET)... etc...
# Gur said on July 5, 2004 12:00 AM:
Does anybody know hnow can i do the same thing but in pocket pc? there is dont have System.Diagnostic.Process

Thanks
# FAQ C# (par Yannick Lejeune) said on August 23, 2005 5:10 AM:
# Because Its Possible.com » Blog Archives » links for 2007-03-10 said on March 10, 2007 12:35 AM:

PingBack from http://blog.becauseitspossible.com/2007/03/10/links-for-2007-03-10/

# Web 2.0 - Social Media - Internet News - Blogging » How can I run another application or batch file from my Visual C# … said on November 7, 2007 12:53 PM:

PingBack from http://hyiplive.org/how-can-i-run-another-application-or-batch-file-from-my-visual-c

# KATALOG-STRON.AZ.PL - CIEKAWE INFORMACJE Z RÓŻNYCH STRON » running a batch file from a command window launched from a c sharp program said on March 27, 2008 12:33 PM:

PingBack from http://katalog-stron.az.pl/?p=1325

# esecuzione processi in background | hilpers said on January 18, 2009 7:50 AM:

PingBack from http://www.hilpers.it/2657457-esecuzione-processi-in-background

# Eseguire uno script in DOS | hilpers said on January 21, 2009 9:23 AM:

PingBack from http://www.hilpers.it/2656120-eseguire-uno-script-in-dos

# xvsb said on March 5, 2009 1:00 PM:

PingBack from http://xvsb.wordpress.com/2009/03/05/16/

# C Frequently Asked Questions How can I run another application or | debt consolidator said on June 15, 2009 4:38 PM:

PingBack from http://mydebtconsolidator.info/story.php?id=19676

# Matt Buchwald said on November 16, 2009 4:12 PM:

If you don't want to force your users to reuse/not reuse via a setting en IE, why not code the option into your application? That way if they want to reuse in IE, but have only your application not reuse, then they can dictate that.

# khaled Y said on November 27, 2009 7:57 PM:

hi all thanx for this code it was helpful for me ,but i want something else ,iwant this process start into my app form can anybody help me

for example i  dont wanna open excel separated on my form i wanna open excel into my form app into panal

# Rao said on December 9, 2009 9:15 PM:

Hi all, i want to launch an application-1 within a UI control of another application-2. Is there anyway i can do it?

# chary said on January 30, 2010 10:55 AM:

batch file is in server and it execute a python a script on the server. How to do it from the client browser through dotnet web application

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

This Blog

Syndication

Page view tracker