我正在为命令行可执行文件编写一个包装类.这个exe接受来自stdin的输入,直到我在命令提示符shell中命中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();
Run Code Online (Sandbox Code Playgroud)
但是,当我手动运行exe时,输出总是为空,即使我从stdout获取数据.编辑:这是c#2.0顺便说一句
System.Diagnostics.Process公开名为StandardInput的StreamWriter,据我所知,它只接受字符.
但我也需要发送击键,有些击键不能很好地映射到角色.
我该怎么办?