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

我正在为命令行可执行文件编写包装器类。该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


动漫人物
浏览 1438回答 3
3回答

肥皂起泡泡

我实际上只是想出了答案。谢谢你们的回答,但事实证明,我要做的就是:p.StandardInput.Close()这使我产生的程序完成了从stdin的读取并输出了我所需要的。
打开App,查看更多内容
随时随地看视频慕课网APP