Question

I created a windows form application that runs a ping command when a button is pressed. Is there any way that I could copy the output from the CMD window to the clipboard?

This is what I am using to run the CMD process:

Process process = new System.Diagnostics.Process();
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = pingData;
process.StartInfo = startInfo;
process.Start();

P.S. The line to hide the CMD window is commented so I can see the window for debugging purposes.

Thank you!

Was it helpful?

Solution

I think you can use the following simple solution:

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c ping 192.168.1.1"; //or your thing
p.Start();

p.WaitForExit();
string result = p.StandardOutput.ReadToEnd();

System.Windows.Forms.Clipboard.SetText(result);

OTHER TIPS

you may want to do it in 2 steps

  1. process.StartInfo.RedirectStandardOutput = true; string allText = process.StandardOutput.ReadToEnd();

  2. System.Windows.Forms.Clipboard.SetText(allText); // i think

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top