C# 按下退出键时退出控制台应用程序

C# 按下退出键时退出控制台应用程序,c#,C#,请任何人帮我,我想自定义我的控制台程序,只有当用户按下键盘上的退出键时才能退出。我感谢你的帮助。 C控制台应用程序。试试像这样的smth using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Runtime.InteropServices; names

请任何人帮我,我想自定义我的控制台程序,只有当用户按下键盘上的退出键时才能退出。我感谢你的帮助。 C控制台应用程序。

试试像这样的smth

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication
{

         [DllImport("user32.dll")]
        static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
        [DllImport("user32.dll")]
        static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
        internal const UInt32 SC_CLOSE = 0xF060;
        internal const UInt32 MF_ENABLED = 0x00000000;
        internal const UInt32 MF_GRAYED = 0x00000001;
        internal const UInt32 MF_DISABLED = 0x00000002;
        internal const uint MF_BYCOMMAND = 0x00000000;


        static void Main(string[] args)
        {
            EnableCloseButton(this, false);
        }

        public static void EnableCloseButton(IWin32Window window, bool bEnabled)
        {
            IntPtr hSystemMenu = GetSystemMenu(window.Handle, false);
            EnableMenuItem(hSystemMenu, SC_CLOSE, (uint)(MF_ENABLED | (bEnabled ? MF_ENABLED : MF_GRAYED)));
        }


    }

你能给我们看一下你的程序代码吗?Console.ReadKey…你还想禁用单击控制台窗口上的红色退出按钮吗?我认为,如果用户按下控制台窗口上的关闭按钮,重复操作不包括中止关闭的情况。让我们稍等片刻,等待上面我的评论的答案查看下面答案中的评论,我应该向以前关闭应用程序的用户道歉,因为它是重复的。这是否会阻止按关闭按钮退出应用程序?请重新阅读questionPost变量以禁用关闭按钮。但我不认为你是如何阻止按下ctrl+alt+del并从那里关闭的。最好运行一些服务,它会在应用程序关闭时随时启动。链接不错,没有足够的时间来测试这些东西。然而,这似乎是相当有争议的。在没有编译器帮助的情况下,静态方法中的关键字this似乎是一个明显的错误。谢谢!但我实际上并不是要禁用关闭按钮,而是这样的: