C#:将ctrl + c发送到以Process.Start()启动的控制台程序? [英] C#: send ctrl+c to console program started with Process.Start()?

查看:432
本文介绍了C#:将ctrl + c发送到以Process.Start()启动的控制台程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

我无法弄清楚如何模拟向外部程序发送Ctrl + C。当我通过CMD手动运行程序时,当我按ctrl + c时,它会中止并询问我是否要在完全关闭之前保存。我试图通过C#模拟这个,但似乎不起作用。

I can't figure out how to simulate sending Ctrl+C to an external program. When I run the program manually through CMD, when I press ctrl+c it will abort and ask me if I want to save before it shuts down completely. I'm trying to simulate this through C# but it doesn't seem to work.

这是我现在在做的:

// Create new process object
process = new Process();

// Setup event handlers
process.EnableRaisingEvents = true;
process.OutputDataReceived += OutputDataReceivedEvent;
process.ErrorDataReceived += ErrorDataReceivedEvent;
process.Exited += ProgramExitedEvent;

// Setup start info
ProcessStartInfo psi = new ProcessStartInfo
{
    FileName = ExePath,
    UseShellExecute = false, // Must be false to redirect IO
    RedirectStandardOutput = false,
    RedirectStandardError = false,
    RedirectStandardInput = true,
    Arguments = arguments
};

process.StartInfo = psi;

// Start the program
process.Start();

process.StandardInput.Write( "\x3" ); // 0x3 is Ctrl+C according to ASCII table

程序没有回应这个,只是继续。问题是Windows在控制台中执行Ctrl + C时实际上并不向输入流发送Ctrl + C,而是向进程发送一个中断我以为发送\x3到输入流是最好的Windows在控制台中按Ctrl + C的功能。我错了吗?

The program doesn't respond to this and just continues. Is the problem that Windows actually doesn't send Ctrl+C to the input stream when doing Ctrl+C in the console, but instead sends an "interrupt" to the process? I thought that sending "\x3" to the input stream is EXACTLY what Windows does when one presses Ctrl+C in the console. Am I wrong?

推荐答案

这个问题已经在以前的场合回答过,其中我发布了关于SO 这里

This question has been answered on a prior occasion in which I posted a detailed answer on SO here.

这篇关于C#:将ctrl + c发送到以Process.Start()启动的控制台程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆