0

I have read the many questions on here about capturing the output from a command in C#, but they all use the StartInfo.UseShellExecute = false property to redirect output. But this doesn't work for me because I still want the console window to open an display the output there as well.

This allows me to capture the output to a file, but doesn't display the console.

using System.Diagnostics;

Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.Arguments = "/C ping 8.8.8.8 -t";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;
cmd.Start();
cmd.BeginOutputReadLine();
.
.
.

This displays the console, but doesn't allow me to capture the output.

using System.Diagnostics;

Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.Arguments = "/C ping 8.8.8.8 -t";
cmd.Start();
.
.
.

My goal is to launch multiple command windows, each running a given command. However, I also want the console output saved to a log file. One log file per command windows. It is trivial to start the command windows and let them run using their own STDOUT, OR redirect the output to a file. But how can I do both?

I have even gone as far as to try to redirect the output and launch my own WinForms that look like consoles to display the output, but was stopped because each was in a separate process and could not write to the GUI thread.

Free_D
  • 577
  • 3
  • 16
  • 1
    Possible duplicate of [Mirroring console output to a file](https://stackoverflow.com/questions/420429/mirroring-console-output-to-a-file) – JuanR Jul 24 '17 at 17:26
  • You need to use `async` and `await` for the WinForms approach this passes back handling to the GUI. Also, if you are saving to a log file, why do you need it to display in the cmd window? – interesting-name-here Jul 24 '17 at 17:27
  • @Juan I cannot control how the command inside the new process will direct it's output – Free_D Jul 24 '17 at 17:45

1 Answers1

0

Well, for something like a 'ping' command, a cmd window is not needed and can be completed from c# itself. Here is the code below:

using System.Net.NetworkInformation; //required for ping

Console.Write("Address to ping: ");
string nameOrAddress = Console.ReadLine();
bool pingable = false;
Ping pinger = new Ping();

try
{
    PingReply reply = pinger.Send(nameOrAddress);
    pingable = reply.Status == IPStatus.Success;

    if(pingable == true) 
    {
        Console.WriteLine("Connected successfully");
    }
    else if(pingable == false) 
    {
        Console.WriteLine("Unable to connect");
    }
}
catch (PingException)
{
    Console.WriteLine("Unknown Error. Try again. Maybe an incorrect address");
}
mason
  • 31,774
  • 10
  • 77
  • 121
Nick
  • 23
  • 7
  • If you type an address in an incorrect format for example if, if i entered "dfsdff" it will fall through to the else because it isn't in a format that can be read by pingable. Test it out, this is the code that I used for my program and it worked with no problems. – Nick Jul 24 '17 at 20:41
  • Again, run the code for yourself and see it is reachable. I have tested it and that code is reached otherwise it wouldn't be included. – Nick Jul 24 '17 at 20:44
  • Didn't notice the catch statement. Sorry! – Nick Jul 24 '17 at 20:49
  • Fixed! Sorry again – Nick Jul 24 '17 at 20:52