Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7694123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:14:50+00:00 2026-05-31T21:14:50+00:00

I am writing a C# app that needs to upload a file when the

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:14:51+00:00Added an answer on May 31, 2026 at 9:14 pm

    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:

    class Program
    
    {
        private static bool isclosing = false;
    
        static void Main(string[] args)
        {
            SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
    
            Console.WriteLine("CTRL+C,CTRL+BREAK or suppress the application to exit");
    
            while (!isclosing) ;
        }
    
        private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
        {
            // Put your own handler here
    
            switch (ctrlType)
            {
                case CtrlTypes.CTRL_CLOSE_EVENT:
                    isclosing = true;
                    Console.WriteLine("Program being closed!");
                    break;
            }
    
            return true;
        }
    
        #region unmanaged
        // Declare the SetConsoleCtrlHandler function
        // as external and receiving a delegate.
    
        [DllImport("Kernel32")]
        public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
    
        // A delegate type to be used as the handler routine
        // for SetConsoleCtrlHandler.
        public delegate bool HandlerRoutine(CtrlTypes CtrlType);
    
        // An enumerated type for the control messages
        // sent to the handler routine.
    
        public enum CtrlTypes
        {
            CTRL_C_EVENT = 0,
            CTRL_BREAK_EVENT,
            CTRL_CLOSE_EVENT,
            CTRL_LOGOFF_EVENT = 5,
            CTRL_SHUTDOWN_EVENT
        }
    
        #endregion
    }
    

    It does exactly what you need.

    Greetings,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an iPhone app that needs to be able to upload photos
I'm writing an app that needs some JNI code. This code involves floating point
I'm writing an android app that needs to copy a file to the /system
I'm writing an App and an accopmanying widget that needs data. This data is
I'm fairly new to Java and am writing an app that needs an XML
I'm writing an winforms app that needs to set internet explorer's proxy settings and
I'm writing a Windows .NET app that needs a WYSIWYG text editor control that
I'm writing a simple web app in PHP that needs to have write access
I am writing an application that needs to bring window of an external app
I am writing an app that needs to capture a users signature in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.