C # program calls external program

Source: Internet
Author: User

/*************************************** ******************

* Function: Use the C # program to call the Windows Notepad program for editing.
* A text file named test.txt.
*
* In the entire program, system. Diagnostics. process. Start (Info)
* Is a statement.
* Simply execute an external program. You can use the following code:
* System. Diagnostics. process. Start (
* "External program name", "Startup parameter ");

*
**************************************** *****************/

1 using system;
2
3 class test
4 {
5 static void main ()
6 {
7
8 // declare a Program Information Class
9 system. Diagnostics. processstartinfo info = new system. Diagnostics. processstartinfo ();
10
11 // set the external program name
12 info. filename = "notepad.exe ";
13
14 // set the startup sequence of the program to test.txt
15 info. Arguments = "test.txt ";
16
17 // set the external program working directory to c :\
18 info. workingdirectory = "C :\\";
19
20 // declare a program class
21 system. Diagnostics. Process proc;
22
23 try
24 {
25 //
26 // start the external program
27 //
28 proc = system. Diagnostics. process. Start (Info );
29}
30 catch (system. componentmodel. win32exception E)
31 {
32 console. writeline ("the system cannot find the specified program file. \ R {0} ", e );
33 return;
34}
35
36 // print the start time of the external program
37 console. writeline ("Start Time of external program execution: {0}", Proc. starttime );
38
39 // wait 3 seconds
40 Proc. waitforexit (3000 );
41
42 // force terminate an external program if it has not ended
43 If (Proc. hasexited = false)
44 {
45 console. writeline ("the main program forcibly terminates the running of the external program! ");
46 Proc. Kill ();
47}
48 else
49 {
50 console. writeline ("the external program Exits normally! ");
51}
52 console. writeline ("End Time of the external program: {0}", Proc. exittime );
53 console. writeline ("returned value of an external program when it stops running: {0}", Proc. exitcode );
54}
55}
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.