我创建了一个Windows窗体应用程序,在按下按钮时运行ping命令。有什么办法可以将CMD窗口的输出复制到剪贴板吗?
这就是我用来运行CMD过程的原因:
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。隐藏CMD窗口的行已注释,因此我可以看到该窗口以进行调试。
谢谢!
答案 0 :(得分:0)
我认为您可以使用以下简单的解决方案:
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);
答案 1 :(得分:-1)
您可能希望分两步完成
process.StartInfo.RedirectStandardOutput = true; string allText = process.StandardOutput.ReadToEnd();
System.Windows.Forms.Clipboard.SetText(allText); //我想