[Import] How can I run another application or batch file from my Visual C #. Net code?

Source: Internet
Author: User

Posted by: Duncan macenzie, 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 VoidSimplerun_click (ObjectSender, 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 VoidLaunchill_click (ObjectSender, system. eventargs e ){
 StringTargetUrl = @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 portAn earlier vb faq post... Just in case you thought you had seen it already in your feeds...


Source: http://blogs.msdn.com/csharpfaq/archive/2004/06/01/146375.aspx

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.