0

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?

Stefan
  • 17,448
  • 11
  • 60
  • 79
Noob
  • 3
  • 1
  • Does this answer your question? [How do I create a MessageBox in C#?](https://stackoverflow.com/questions/49147/how-do-i-create-a-messagebox-in-c) – Yogi Apr 03 '22 at 13:50
  • No i need Display cmd output in MessageBox. – Noob Apr 03 '22 at 13:51

1 Answers1

1

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.
Stefan
  • 17,448
  • 11
  • 60
  • 79
  • Hello im taking this error: "Severity Code Description Project File Line Hide Status Error CS1061 'Process' does not contain a definition of 'WaitForExitAsync' and no accessible extension method 'WaitForExitAsync' was found that accepts a first argument of type 'Process' (could be missing a using directive or assembly reference?)" – Noob Apr 03 '22 at 14:24
  • Is there a basic code for this? – Noob Apr 03 '22 at 14:24
  • Are you using .net 6? Otherwise I'll add an extention method – Stefan Apr 03 '22 at 14:40
  • Alternatively you can drop the "async" part. Let me adjust the method. – Stefan Apr 03 '22 at 14:41
  • @Noob: updated and dropped the async part. – Stefan Apr 03 '22 at 14:43