C's method of executing batch commands dynamically

  • 2020-12-10 00:49:13
  • OfStack

The example in this article shows how C# dynamically executes batch commands. Share to everybody for everybody reference. The specific methods are as follows:

C# uses the following functions when executing series 1 console commands dynamically and allowing the results to be displayed in real time. The results can be achieved are:

Continuous input: The console can continue to use the input stream to write subsequent commands
Output of large data volume: the output of large data volume will not cause the program to block
API friendly: Just type the command string you want to execute

The function prototype is:

/// <summary>
/// Open the console to perform the splicing completed batch command string
/// </summary>
/// <param name="inputAction"> Command delegate methods that need to be executed: each invocation <paramref name="inputAction"/> Parameters are executed 1 time </param>
private static void ExecBatCommand(Action<Action<string>> inputAction)

Use examples are as follows:

ExecBatCommand(p =>
{
    p(@"net use \\10.32.11.21\ERPProject yintai@123 /user:yt\ERPDeployer");
    // The commands that are written sequentially will be displayed in the console window
    p("exit 0");
});

Note: After executing the required command, you finally need to call the exit command to exit the console. The goal is to continue typing until the user executes the exit command exit 0, and the exit command must be the last command, or the program will fail.

The following is the source code of the batch execution function:

/// <summary>
/// Open the console to perform the splicing completed batch command string
/// </summary>
/// <param name="inputAction"> Command delegate methods that need to be executed: each invocation <paramref name="inputAction"/> Parameters are executed 1 time </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();
    }
}

Hopefully, this article has helped you with your C# programming.


Related articles: