C# Frequently Asked Questions

The C# team posts answers to common questions

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

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...

Published Tuesday, June 01, 2004 11:54 PM by CSharpFAQ
Filed under:

Comments

 

Ryan Farley said:

"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
June 2, 2004 9:44 AM
 

Duncan Mackenzie said:

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.
June 2, 2004 10:23 AM
 

Ryan Farley said:

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.
June 2, 2004 11:01 AM
 

Shawn said:

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
June 2, 2004 5:59 PM
 

Marc: My Words said:

June 3, 2004 10:13 AM
 

Marc: My Words said:

June 3, 2004 11:05 AM
 

Tom Krueger said:

June 26, 2004 2:21 AM
 

James Mc Kenzie said:

Under what user context does this application start under?, What permissions would it have?
June 29, 2004 9:28 AM
 

Duncan Mackenzie said:

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...
June 29, 2004 9:32 AM
 

Gur said:

Does anybody know hnow can i do the same thing but in pocket pc? there is dont have System.Diagnostic.Process

Thanks
July 5, 2004 12:00 AM
 

FAQ C# (par Yannick Lejeune) said:

May 17, 2005 10:05 AM
 

FAQ C# (par Yannick Lejeune) said:

May 17, 2005 10:06 AM
 

FAQ C# (par Yannick Lejeune) said:

August 23, 2005 5:10 AM
 

Because Its Possible.com » Blog Archives » links for 2007-03-10 said:

March 10, 2007 12:35 AM
 

Web 2.0 - Social Media - Internet News - Blogging » How can I run another application or batch file from my Visual C# … said:

November 7, 2007 12:53 PM
 

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:

March 27, 2008 12:33 PM
 

esecuzione processi in background | hilpers said:

January 18, 2009 7:50 AM
 

Eseguire uno script in DOS | hilpers said:

January 21, 2009 9:23 AM
 

xvsb said:

March 5, 2009 12:46 PM
 

xvsb said:

March 5, 2009 12:59 PM
 

xvsb said:

March 5, 2009 1:00 PM
 

C Frequently Asked Questions How can I run another application or | debt consolidator said:

June 15, 2009 4:38 PM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker