title: Run code on console close?
body: <p>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).</p>
  
  <p>How could I do this?</p>
  
  <pre><code>AppDomain.CurrentDomain.ProcessExit += new EventHandler (OnExit);
  </code></pre>
  
  <p>Only runs when I issue the <code>exit</code> command to the console, not when I hit the red close button.</p>
  
  <p>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).</p>