C# .NET - display command being executed by Process Start()

Asked By Miguel Martinez on 14-Aug-05 10:00 AM
Hi. 

When I use Process.Start("cmd.exe", "ping 127.0.0.1") or Process.Start("cmd.exe", "/C ping 127.0.0.1") it open up the command prompt and automatically displays the output, like :

Pinging 127.0.0.1 with 32 bytes of data: 

Reply from 127.0.0.1..


and so on...

What I need is for the command prompt to display the location of the cmd.exe and the command being executed, similar to manually typing it. For example the above output would now be:

C:\Windows\system32\ping 127.0.0.1    <<-- this line is now included..
Pinging 127.0.0.1 with 32 bytes of data: 

Reply from 127.0.0.1..

and so on...

Is it possible to do that? I've tried experimenting on using ProcessStartInfo and setting its WorkingDirectory property to C:\Windows\system32\ but I still won't work. 

Kindly advise. Thanks.

How about using - Asked By Aarthi Saravanakumar on 14-Aug-05 10:14 AM

Process.Start("cmd.exe", "C:\Windows\System32\ping 127.0.0.1")

I still doesn't work - Asked By Miguel Martinez on 14-Aug-05 11:00 AM

Actually, all I want is for the user to see the command being executed and not just the output... 

Process.Start("cmd.exe", "C:\Windows\System32\ping 127.0.0.1") or
Process.Start("cmd.exe", "ping 127.0.0.1")

would just display the output of the command excluding the line where you'll see the the exact command on top. Sorry for my stupid question, but I just really need the user to see the command + output of the command..

You can use something like this - Asked By Jon Wojtowicz on 14-Aug-05 11:26 AM

("cmd.exe", @"/C ECHO %windir%\system32\ping 127.0.0.1 && %windir%\system32\ping 127.0.0.1");

Not quite as clean but will display what you want.
You can redirect the StandardOutput - Asked By Peter Bromberg on 14-Aug-05 03:31 PM
to a stream and capture this information.

Look up the documentation for the Process class and you'll see an example.

You can get the output back in a StreamReader if you like.
Thanks - Asked By Miguel Martinez on 15-Aug-05 09:35 AM
Thanks. This would do fine. :)
Thanks - Asked By Miguel Martinez on 15-Aug-05 09:37 AM
Thanks for all your help. But Jon's solution already solved my problem. Thanks!