C # programs Call external programs

Source: Internet
Author: User
Program/*
* Programming Language: visualstudio.netc# (BETA2)
* Author: Dee Bo
* Function: Call Windows Notepad program with C # program to edit a
* A text file named Test.txt.
*
* System.Diagnostics.Process.Start (Info) throughout the program
* is the main statement.
* If you are only performing an external program separately, you can use one of the following code:
*system.diagnostics.process.start (
* "External program name", "Start parameter");
*/

Usingsystem;

Classtest
{
Staticvoidmain ()
{

Declaring a program information class
System.diagnostics.processstartinfoinfo=newsystem.diagnostics.processstartinfo ();

Set External program name
Info.filename= "notepad.exe";

Set the external program's startup parameters (command-line arguments) to Test.txt
Info.arguments= "Test.txt";

Set external program working directory for c:\\
info.workingdirectory= "c:\\\\";

Declaring a program class
System.Diagnostics.ProcessProc;

Try
{
//
Start an external program
//
Proc=system.diagnostics.process.start (Info);
}
catch (System.ComponentModel.Win32Exceptione)
{
Console.WriteLine ("The system cannot find the specified program file. \\r{0} ", E);
Return
}

Print out the start time of an external program
Console.WriteLine ("Start of external program execution time: {0}", proc.starttime);

Wait 3 seconds.
Proc.waitforexit (3000);

If this external program does not finish running, it is forced to terminate
if (Proc.hasexited==false)
{
Console.WriteLine ("Forced by the main program to terminate the operation of the external program!") ");
Proc.kill ();
}
Else
{
Console.WriteLine ("Normal exit by external program!") ");
}
Console.WriteLine ("End Run time of external program: {0}", proc.exittime);
Console.WriteLine ("Return value of external program at end of run: {0}", Proc.exitcode);
}
}


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.