相关疑难解决方法(0)

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

我正在为命令行可执行文件编写一个包装类.这个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顺便说一句

c# command-line process .net-2.0

28
推荐指数
4
解决办法
5万
查看次数

如何将键而不是字符发送到进程?

System.Diagnostics.Process公开名为StandardInput的StreamWriter,据我所知,它只接受字符.

但我也需要发送击键,有些击键不能很好地映射到角色.

我该怎么办?

.net c# keyboard process streamwriter

7
推荐指数
2
解决办法
2万
查看次数

标签 统计

c# ×2

process ×2

.net ×1

.net-2.0 ×1

command-line ×1

keyboard ×1

streamwriter ×1