ASP. NET, C # call external executable EXE file--Multiple scenarios

Source: Internet
Author: User

One

Try        {            //Method One//call your own exe pass parameters//Process proc = new process (); //Proc.            Startinfo.filename = @ "D:\\hotelsoft\\zk.exe"; //Proc.            Start (); //Thread.Sleep (+);//pause for 3 seconds//foreach (System.Diagnostics.Process pro inSystem.Diagnostics.Process.GetProcessesByName ("ZK"))            //{            //find and close a process//Pro.            Kill (); //}            //Method TwoProcessStartInfo StartInfo =NewProcessStartInfo (@"D:\\hotelsoft\\zk.exe"); Startinfo.windowstyle=processwindowstyle.minimized;        Process.Start (StartInfo); }        Catch(Exception ex) {Response.Write (ex).        ToString ()); }

Two

Calling external program cmd command line
Process P = new process ();
p.StartInfo.FileName = "cmd.exe";
P.startinfo.useshellexecute = false;
P.startinfo.redirectstandardinput = true;
P.startinfo.redirectstandardoutput = true;
P.startinfo.createnowindow = false;
P.start ();
//Enter command to Cmd.exe
P.standardinput.writeline ("CD c:\\inetpub\\wwwroot\\paicdom\\pawebservice\\paweb\\manage\\exportcsv");
CMD calls the Ociuldr.ex again.
P.standardinput.writeline ("Ociuldr.exe");
P.standardinput.writeline ("Exit"); Need to have this sentence, or the program will hang up the machine
String output = P.standardoutput.readtoend (); This sentence can be used to get the output of the execution command.

I called in ASP. I always wanted to see the execution window, but no matter how I set the parameters, I couldn't see them. I don't know how.

Three

C # programs Call this need to generate a progress class that provides the properties and events that are used to invoke the EXE executable. System.Diagnostics.Process Pexecuteexe = new System.Diagnostics.Process ();
PExecuteEXE.StartInfo.FileName = @ "E:\Delphi.exe";
pExecuteEXE.StartInfo.Arguments = "' Paramstr1 paramstr2,paramstr3 '";
Pexecuteexe.start ();
Pexecuteexe.waitforexit ();//wait indefinitely for completion
Pexecuteexe.waitforexit (10000);//wait for a maximum of 10 seconds to complete. The Delphi executable is called the program, mainly receives the parameter information, executes the program, because the program executes the program completion cannot return to the caller information, can only write the information to a place waits for the caller to read.
Procedure Tform1.button1click (Sender:tobject);

Var
I:integer;
Begin
Self. Caption: = ";
For i:=0 to ParamCount do
Begin
Self. Caption: =self. caption+ ' [' +inttostr (i) + ': ' +paramstr (i) + '] ';
End

End

Exit when finished.

Here is an example of a simple invocation that can be emulated:
* Function: Call Windows Notepad program through 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 simply executing an external program, you can use one of the following code:
* System.Diagnostics.Process.Start (
* "External program name", "Startup parameter");
*/

Using System;

Class Test
{
static void Main ()
{

Declaring a program information class
System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo ();

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

Set the startup parameters of the external program (Command line arguments) to Test.txt
info.arguments = "Test.txt";

Set external program working directory to c \
Info.workingdirectory = "c:\\";

Declaring 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 out the start execution time of the external program
Console.WriteLine ("Start execution time of the external program: {0}", proc.starttime);

Wait 3 seconds
Proc.waitforexit (3000);

If the external program does not end up running, it will be forcibly terminated.
if (proc.hasexited = = False)
{
Console.WriteLine ("The main program forcibly terminates the operation of the external program!") ");
Proc.kill ();
}
Else
{
Console.WriteLine ("Exit by external program normally!") ");
}
Console.WriteLine ("End run time for external programs: {0}", proc.exittime);
Console.WriteLine ("The return value of the external program at the end of runtime: {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.