[C # FAQ] C # How do I start another application or batch processing program in code?

Source: Internet
Author: User
Original URL: How can I run another application or batch file from my Visual C #. Net code?
Posted by: Duncan macenzie, msdn
This post applies to Visual C #. NET 2002/2003

If you want to run a command line Program , Or open a Windows application, or open the default web browser or email client, how do you # Code To implement this function?
In the following example, you can use the classes and methods in system. Diagnostics. process to complete the same task, or even do more.
Example 1: Regardless of the output result, you only need to run a command line program:

 Private    Void  Simplerun_click (  Object  Sender, system. eventargs E)  {
System. Diagnostics. process. Start (@ "C: \ listfiles. Bat" );
}

Example 2. Get the program running result and wait until the program is stopped (synchronous running program) 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. use the default browser on your machine to display the URL
private void launchunl_click ( Object sender, system. eventargs e) {
string TargetUrl =@ http://www.duncanmackenzie.net ;< br> system. diagnostics. process. start (TargetUrl);
}< br>
In my opinion, the URL is displayed in a browser, for example The method is more reasonable than starting IE and using URL as the parameter.
the code in Example 3 starts the user's default browser, rather than IE. In this way, you are more likely to bring users the experience they want,
and you can use browsers with the latest connection information.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.