代码之家  ›  专栏  ›  技术社区  ›  Kevlar

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

  •  22
  • Kevlar  · 技术社区  · 16 年前

    我正在为命令行可执行文件编写一个包装器类。此exe接受来自的输入 stdin 直到我击中 Ctrl+C 在命令提示符shell中,在这种情况下,它将输出打印到 stdout 基于输入。我想模拟一下 Ctrl+C 按C#代码,将kill命令发送到.NET Process 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();
    

    输出总是空的,即使我从 stdout 当我手动运行exe时。

    编辑 :顺便说一下,这是C#2.0。

    5 回复  |  直到 5 年前