I am writing a C# app that needs to upload a file when the console is closed (be it via the X button, or the computer is shut down).
How could I do this?
AppDomain.CurrentDomain.ProcessExit += new EventHandler (OnExit);
Only runs when I issue the exit command to the console, not when I hit the red close button.
Please only answer if the solution runs both when the console is closed via the X button, and when the computer is shut down (normally via Windows, I know you can’t if the power is pulled xD).
You have to invoke the WIN32 API, to do this, just have a look at this post here
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/707e9ae1-a53f-4918-8ac4-62a1eddb3c4a/
I copied the relevant code for you from there:
It does exactly what you need.
Greetings,