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.