C # program calls external program

Source: Internet
Author: User

/*
* Programming language: Visual Studio. net c # (Beta 2)
* Author: dipolwe
* Function: Use the C # program to call the Windows Notepad program to edit
* A text file named test.txt.
*
* In the entire program, System. Diagnostics. Process. Start (Info)
* Is the main statement.
* If you only execute an external program separately, you can use the following code:
* System. Diagnostics. Process. Start (
* "External program name", "Startup parameter ");
*/

Using System;

Class test
{
Static void Main ()
{

// Declare a Program Information Class
System. Diagnostics. ProcessStartInfo Info = new System. Diagnostics. ProcessStartInfo ();

// Set the external program name
Info. FileName = "notepad.exe ";

// Set the startup sequence of the program to test.txt.
Info. Arguments = "test.txt ";

// Set the external program working directory to C:
Info. WorkingDirectory = "C :\";

// Declare a program class
System. Diagnostics. Process Proc;

Try
{
//
// Start an external program
//
Proc = System. Diagnostics. Process. Start (Info );
}
Catch (System. ComponentModel. Win32Exception e)
{
Console. WriteLine ("the specified program file cannot be found. {0} ", e );
Return;
}

// Print the start time of the external program
Console. WriteLine ("Start Time of the external program: {0}", Proc. StartTime );

// Wait 3 seconds
Proc. WaitForExit (3000 );

// Forcibly terminate an external program if it has not ended
If (Proc. HasExited = false)
{
Console. WriteLine ("the main program forcibly terminates the running of the external program! ");
Proc. Kill ();
}
Else
{
Console. WriteLine ("the external program Exits normally! ");
}
Console. WriteLine ("external program end running time: {0}", Proc. ExitTime );
Console. WriteLine ("returned value of an external program when it stops running: {0}", Proc. ExitCode );
}
}

Related Article

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.