gpt4 book ai didi

c# - 从通过 C# Windows 窗体执行的 CMD 窗口复制文本

转载 作者:太空宇宙 更新时间:2023-11-03 15:58:45 25 4
gpt4 key购买 nike

我创建了一个 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();

附言隐藏 CMD 窗口的行已被注释,因此我可以看到用于调试目的的窗口。

谢谢!

最佳答案

我认为您可以使用以下简单的解决方案:

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);

关于c# - 从通过 C# Windows 窗体执行的 CMD 窗口复制文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22389147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com