Search code examples
c#winformsshutdown

Run code on WinForms app close and computer shutdown?


I want my C# WinForms app to run some code when either the app is closed, or the computer is shutdown (via the startmenu, I know it wont work if they pull the plug :P).

Basically I have some log files that I need the app to upload to an FTP server on shutdown.

How can I do this?


Solution

  • You can detect your application shutting down by either intercepting the FormClosing event.

    You can detect widows shutdown, logoff, suspend, etc using SystemEvents. I recommend not using the system events as you may not get enough time to perform an upload to a remote server (I think the default is 3 seconds).