C #: send ctrl + c to a console program launched with Process.Start ()?

Possible duplicate:
How to send ctrl + c to a process in C #?

I cannot 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 interrupts and asks me if I want to save before it completely shuts down. I am trying to simulate this through C #, but it does not work.

Here is what I am doing now:

// 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 

The program does not respond to this and only continues. The problem is that Windows does not actually send Ctrl + C to the input stream when doing Ctrl + C in the console, but instead sends an โ€œinterruptโ€ to the process? I thought sending "\ x3" to the input stream is EXACTLY what Windows does when you press Ctrl + C in the console. I am wrong?

+5
source share
1 answer

This question was answered on a previous case in which I posted a detailed answer to SO here .

0
source

Source: https://habr.com/ru/post/923736/


All Articles