5

Possible Duplicate:
How do I send ctrl+c to a process in 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.

This 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 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?

Buaban
  • 5,029
  • 1
  • 17
  • 33
johnrl
  • 873
  • 3
  • 14
  • 18
  • Your console is probably catching the `^C` and sending a signal of some kind to the program. You need to emulate that behaviour; on Unixy systems it would be `SIGINT`, I don't know that much about windows, I'm afraid. – Carl Norum Mar 25 '10 at 16:57
  • This is one of my theories as well. I don't know what CMD on Windows is sending to my program (if it's not ctrl+c). – johnrl Mar 25 '10 at 17:03

0 Answers0