Search code examples
c#.netconsole

How do I trap Ctrl+C (SIGINT) in a C# console app?


I would like to be able to trap Ctrl+C in a C# console application so that I can carry out some cleanups before exiting. What is the best way of doing this?


Solution

  • See MSDN:

    Console.CancelKeyPress Event

    Article with code samples:

    Ctrl-C and the .NET console application