为了账号安全,请及时绑定邮箱和手机立即绑定

如何在C#中将ctrl + c发送到进程?

如何在C#中将ctrl + c发送到进程?

动漫人物 2019-11-19 10:34:36
我正在为命令行可执行文件编写包装器类。该exe接受来自stdin的输入,直到我在命令提示符外壳中按ctrl + c为止,在这种情况下,它将根据输入将输出打印到stdout。我想模拟ctrl + c按下c#代码,将kill命令发送到.Net进程对象。我试过调用Process.kill(),但是在该进程的StandardOutput StreamReader中似乎没有给我任何东西。可能有什么我做对的事情吗?这是我要使用的代码:ProcessStartInfo info = new ProcessStartInfo(exe, args);info.RedirectStandardError = true;info.RedirectStandardInput = true;info.RedirectStandardOutput = true;info.UseShellExecute = false;Process p = Process.Start(info);p.StandardInput.AutoFlush = true;p.StandardInput.WriteLine(scriptcode);p.Kill(); string error = p.StandardError.ReadToEnd();if (!String.IsNullOrEmpty(error)) {     throw new Exception(error);}string output = p.StandardOutput.ReadToEnd();但是,即使我手动运行exe时从stdout获取数据,输出也始终为空。编辑:这是c#2.0 btw
查看完整描述

3 回答

?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

我实际上只是想出了答案。谢谢你们的回答,但事实证明,我要做的就是:


p.StandardInput.Close()

这使我产生的程序完成了从stdin的读取并输出了我所需要的。


查看完整回答
反对 回复 2019-11-19
  • 3 回答
  • 0 关注
  • 1426 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信