C # Methods for dynamic execution of batch commands

Source: Internet
Author: User

The example in this article describes how C # dynamically executes batch commands. Share to everyone for your reference. Here's how:

The following function can be used when C # dynamically executes a series of console commands and allows the results to be displayed in real time. The results can be achieved as follows:

Continuous input: The console can continue to write subsequent commands using the input stream

Large data output: does not cause program blocking due to output from large data volumes

Friendly API: Directly enter the command string you need to execute

The function prototypes are:


The code is as follows:

<summary>

Open the console to perform a concatenation of the completed batch command string

</summary>

<param name= "inputaction" > Command delegate method to execute: Each call to <paramref name= "Inputaction"/> The parameters will be executed once </param>

private static void Execbatcommand (Action<action<string>> inputaction)

Examples of use are:


The code is as follows:

Execbatcommand (p =

{

P (@ "net use \\10.32.11.21\ERPProject [email protected]/user:yt\erpdeployer");

Commands written here will be shown in the console window in turn

P ("Exit 0");

});

Note: After executing the required commands, you will need to call the Exit command to exit the console. The purpose of this is to continue to enter the command, know that the user executes exit command exit 0, and the exit command must be the last command, or the program will have an exception.

The following is the batch execution function source code:


The code is as follows:

<summary>

Open the console to perform a concatenation of the completed batch command string

</summary>

<param name= "inputaction" > Command delegate method to execute: Each call to <paramref name= "Inputaction"/> The parameters will be executed once </param>

private static void Execbatcommand (Action<action<string>> inputaction)

{

Process pro = null;

StreamWriter sIn = null;

StreamReader sOut = null;

Try

{

Pro = new Process ();

Pro. Startinfo.filename = "cmd.exe";

Pro. Startinfo.useshellexecute = false;

Pro. Startinfo.createnowindow = true;

Pro. Startinfo.redirectstandardinput = true;

Pro. Startinfo.redirectstandardoutput = true;

Pro. Startinfo.redirectstandarderror = true;

Pro. outputdatareceived + = (sender, E) = Console.WriteLine (E.data);

Pro. errordatareceived + = (sender, E) = Console.WriteLine (E.data);

Pro. Start ();

SIn = Pro. Standardinput;

Sin.autoflush = true;

Pro. Beginoutputreadline ();

Inputaction (value = sin.writeline (value));

Pro. WaitForExit ();

}

Finally

{

if (pro! = null &&!pro. hasexited)

Pro. Kill ();

if (sIn! = null)

Sin.close ();

if (sOut! = null)

Sout.close ();

if (pro! = null)

Pro. Close ();

}

}

I hope this article is helpful to everyone's C # programming.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Methods for dynamic execution of batch commands

This address: http://www.paobuke.com/develop/c-develop/pbk23312.html






Related content C # current system time acquisition and time format in C # The use of enumeration types in programming C # Implement a form that is scaled down by a perimeter center C # Implement a method of finding a group of data
C # Adaptive merging of files Methods C # Monitor folder and automatically water-seal the picture file C # Custom Simplified cookie class Instance C # Ajax paging instance based on database stored procedure

C # Methods for dynamic execution of batch commands

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.