C program calls cmd to execute command

Reprinted from: https://www.cnblogs.com/babycool/p/3570648.html

One method

string str = Console.ReadLine();

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;    //Whether to use the operating system shell to start
p.StartInfo.RedirectStandardInput = true;//Accept input from caller
p.StartInfo.RedirectStandardOutput = true;//Get output information from caller
p.StartInfo.RedirectStandardError = true;//Redirect standard error output
p.StartInfo.CreateNoWindow = true;//Don't show program window
p.Start();//Startup program

//Send input information to the cmd window
p.StandardInput.WriteLine(str + "&exit");

p.StandardInput.AutoFlush = true;
//p.StandardInput.WriteLine("exit");
//Write the command to be executed to standard input. Here & is the symbol of batch command, which means that the previous command executes the subsequent (exit) command no matter whether it is executed successfully or not. If the exit command is not executed, the subsequent call to ReadToEnd() method will die
//Other symbols of the same kind are & & and |, the former indicates that the previous command must be executed successfully before the subsequent command can be executed, and the latter indicates that the previous command must be executed successfully before the latter command can be executed

//Get output information of cmd window
string output = p.StandardOutput.ReadToEnd();

//StreamReader reader = p.StandardOutput;
//string line=reader.ReadLine();
//while (!reader.EndOfStream)
//{
//    str += line + "  ";
//    line = reader.ReadLine();
//}

p.WaitForExit();//Wait for the program to finish executing and exit the process
p.Close();

Console.WriteLine(output);

One thing to be noticed is that after the execution of the previous command, the exit command should be added, otherwise the ReadtoEnd() command will be called later.

For how to run the cmd command as an administrator, refer to How to run programs as administrators - cool kids - blog Park

Another way

This method will "flash" the black window when it is executed.

/// <summary>
/// Function cmd command
/// The command window is displayed
/// </summary>
/// <param name="cmdExe">Specify the full path of the application</param>
/// <param name="cmdStr">Execute command line arguments</param>
static bool RunCmd(string cmdExe, string cmdStr)
{
    bool result = false;
    try
    {
        using (Process myPro = new Process())
        {
            //Specifies the application and command line parameters that the startup process is calling
            ProcessStartInfo psi = new ProcessStartInfo(cmdExe, cmdStr);
            myPro.StartInfo = psi;
            myPro.Start();
            myPro.WaitForExit();
            result = true;
        }
    }
    catch
    {

    }
    return result;
}

/// <summary>
/// Function cmd command
/// Do not show command window
/// </summary>
/// <param name="cmdExe">Specify the full path of the application</param>
/// <param name="cmdStr">Execute command line arguments</param>
static bool RunCmd2(string cmdExe, string cmdStr)
{
    bool result = false;
    try
    {
        using (Process myPro = new Process())
        {
            myPro.StartInfo.FileName = "cmd.exe";
            myPro.StartInfo.UseShellExecute = false;
            myPro.StartInfo.RedirectStandardInput = true;
            myPro.StartInfo.RedirectStandardOutput = true;
            myPro.StartInfo.RedirectStandardError = true;
            myPro.StartInfo.CreateNoWindow = true;
            myPro.Start();
            //If the cmd command fails to execute when there is a space in the calling program path, it can be enclosed in double quotation marks, where two quotation marks represent a quotation mark (escape)
            string str = string.Format(@"""{0}"" {1} {2}", cmdExe, cmdStr, "&exit");

            myPro.StandardInput.WriteLine(str);
            myPro.StandardInput.AutoFlush = true;
            myPro.WaitForExit();

            result = true;
        }
    }
    catch
    {

    }
    return result;
}

Tags: shell

Posted on Tue, 07 Apr 2020 09:59:42 -0700 by martyn