C # program calls external program

xiaoxiao2021-03-06  49

*

* Programming Language: Visual Studio .NET C # (Beta 2)

* Servers: Di Powei

* Features: Edit a Windows Notepad program via C # program

* Text file called Test.txt.

*

* System.diagnostics.Process.Start (Info) throughout the program

* Is the main statement.

* If an external program is only performed separately, you can use the following code:

* System.Diagnostics.Process.start (

* "External program name", "starting parameters");

* /

Using system;

Class test

{

Static void main ()

{

// Declare a program information class

System.Diagnostics.ProcessStartInfo Info = New System.diagnostics.ProcessStartInfo ();

// Set an external program name

Info.filename = "Notepad.exe";

/ / Set the startup parameters of the external program (command line parameters) to test.txt

INFO.Arguments = "test.txt";

// Set the external program work 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 system cannot find the specified program file ./r{ 0}", e);

Return;

}

// Print the start of the external program

Console.writeLine ("The start of the external program: {0}", proc.starttime);

// Wait for 3 seconds

Proc.WaitForexit (3000);

// If this external program does not end running, it is forcibly termination.

IF (proc.hasexited == false)

{

Console.writeline ("is forcibly terminated by the main program!"); ");

Proc.kill ();

}

Else

{

Console.writeline ("exits from external programs!");

}

Console.writeline ("End Run Time of External Programs: {0}", Proc.exittime);

Console.WriteLine ("The external program is ending the return value: {0}", proc.exitcode;

}

}

转载请注明原文地址:https://www.9cbs.com/read-82350.html

New Post(0)