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