Search code examples
c#.netformsmessagebox

How to display the output from cmd in application?


I I need start acmd command in c#, for example: Echo Test.

Next I want to show the output of CMD in an messagebox like this:

MessageBox.Show(output_of_cmd_command);

Is it possible? If so, how?


Solution

  • This will consist of a couple of steps:

    • start the CMD process with the correct arguments
    • capture the CMD output
    • show it in the message box

    I recently did something for Python, by using this function:

    Keep in mind I explicitly suppressed the CMD dialog itself by setting UseShellExecute and CreateNoWindow. If you like you can alter those.

    private string RunCommand(string fileName, string args)
    {
        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = fileName;
        start.Arguments = string.Format("{0}", args);
        start.RedirectStandardOutput = true;
        start.RedirectStandardError = true;
        start.UseShellExecute = false;
        start.CreateNoWindow = true;
        
        var sb = new StringBuilder();
        using (Process process = new Process())
        {
            process.StartInfo = start;
            process.OutputDataReceived += (sender, eventArgs) =>
            {
                sb.AppendLine(eventArgs.Data); //allow other stuff as well
            };
            process.ErrorDataReceived += (sender, eventArgs) => {
            };
    
            if (process.Start())
            {
                process.EnableRaisingEvents = true;
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
    
                process.WaitForExit();
                //allow std out to be flushed
                Thread.Sleep(100);
            }
        }
        return sb.ToString();
    }
    

    Usage:

    var result = RunCommand("path to your cmd.exe", "/C c:\example.bat");
    MessageBox.Show(result);
    

    Here's a listing of the CMD options:

    Starts a new instance of the Windows command interpreter
    
    CMD [/A | /U] [/Q] [/D] [/E:ON | /E:OFF] [/F:ON | /F:OFF] [/V:ON | /V:OFF]
        [[/S] [/C | /K] string]
    
    /C      Carries out the command specified by string and then terminates
    /K      Carries out the command specified by string but remains
    /S      Modifies the treatment of string after /C or /K (see below)
    /Q      Turns echo off
    /D      Disable execution of AutoRun commands from registry (see below)
    /A      Causes the output of internal commands to a pipe or file to be ANSI
    /U      Causes the output of internal commands to a pipe or file to be
            Unicode
    /T:fg   Sets the foreground/background colors (see COLOR /? for more info)
    /E:ON   Enable command extensions (see below)
    /E:OFF  Disable command extensions (see below)
    /F:ON   Enable file and directory name completion characters (see below)
    /F:OFF  Disable file and directory name completion characters (see below)
    /V:ON   Enable delayed environment variable expansion using ! as the
            delimiter. For example, /V:ON would allow !var! to expand the
            variable var at execution time.  The var syntax expands variables
            at input time, which is quite a different thing when inside of a FOR
            loop.
    /V:OFF  Disable delayed environment expansion.