试用了 AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnCurrentDomain_ProcessExit);
不成功。请问有什么方法?

解决方案 »

  1.   

    退出时候弹出 提示框CLOSE(之前要添加using System.Windows.Forms 这个引用 看到效果)  using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;namespace ConsoleApplication2
    {    public delegate bool ConsoleCtrlDelegate(int ctrlType);
        class Program
        {
            [DllImport("kernel32.dll")]
            private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate HandlerRoutine, bool Add);
            //当用户关闭Console时,系统会发送次消息
            private const int CTRL_CLOSE_EVENT = 2;
            //Ctrl+C,系统会发送次消息
            private const int CTRL_C_EVENT = 0;
            //Ctrl+break,系统会发送次消息
            private const int CTRL_BREAK_EVENT = 1;
            //用户退出(注销),系统会发送次消息
            private const int CTRL_LOGOFF_EVENT = 5;
            //系统关闭,系统会发送次消息
            private const int CTRL_SHUTDOWN_EVENT = 6;        static void Main(string[] args)
            {
                Program cls = new Program();
                //Console.ReadKey();
            }
            public Program()
            {
                ConsoleCtrlDelegate consoleDelegete = new ConsoleCtrlDelegate(HandlerRoutine);            bool bRet = SetConsoleCtrlHandler(consoleDelegete, true);
                if (bRet == false) //安装事件处理失败
                {
                    Debug.WriteLine("error");
                }
                else
                {
                    Console.WriteLine("ok");
                    Console.Read();
                }        }        private static bool HandlerRoutine(int ctrlType)
            {
                switch (ctrlType)
                {
                    case CTRL_C_EVENT:
                        MessageBox.Show("C");
                        break;
                    case CTRL_BREAK_EVENT:
                        MessageBox.Show("BREAK");
                        break;
                    case CTRL_CLOSE_EVENT:
                        MessageBox.Show("CLOSE");
                        break;
                    case CTRL_LOGOFF_EVENT:
                        break;
                    case CTRL_SHUTDOWN_EVENT:
                        break;
                }
                //return true;//表示阻止响应系统对该程序的操作
                return false;//忽略处理,让系统进行默认操作
            }
        }}