0

im using c# .net 2.0 Console Application.

How can i prevent my console application from ALT+F4 shortcut?

is there any way to do that?

everywhere there is winform solution.

I need console app solution.

please help me

  • 2
    Why do you want a standard shortcut to not work for you application? – Xerillio Feb 18 '22 at 19:11
  • 4
    You can't. Alt-F4 is handled by the console host, not your application. There is nothing a console application can do to prevent closing of this window, or even do anything in response to it. [See also this question](https://stackoverflow.com/q/11959643/4137916), where multiple workarounds are discussed, none of which (I think) work on modern Windows. – Jeroen Mostert Feb 18 '22 at 19:14
  • @JeroenMostert consider making this comment into an answer. – Asik Feb 18 '22 at 19:17
  • @Jeroen Mostert : There is no working methods their that can prevent exiting my console app. i just want to Disable ALT+F4 from exiting my app by user. I think nothing is immpossible. can u tell me actual solution? – haseakash2010 Feb 18 '22 at 19:52
  • 2
    Well sure, nothing is *impossible*. You can inject code into the system processes to alter the behavior of Windows, for example. All you need is administrator permission and a thorough knowledge of system internals... If you're looking for a *realistic* solution, though, it still remains "don't use a console application". You can use a Windows application that spawns a console application and communicates with it through an IPC mechanism, for example (also described in the linked question). The Windows app can detect if the console app is closed. Or you could use a custom control for output. – Jeroen Mostert Feb 18 '22 at 19:54
  • 1
    Unfortunately I see no reason to disagree with the comment on that question either: that is *also* not possible (in a way that is allowed by documented interfaces). You do seem to have a knack for picking challenges. :P You may want to consider rewriting/reimplementing whatever functionality it is you seek to control here; difficult as that may be, it's probably still less trouble than trying to wrap around it and getting the system to somehow cooperate. – Jeroen Mostert Feb 18 '22 at 19:59
  • @Jeroen Mostert : Thanks man! for your imp time for me. Thanks again bro. i will still searching for these two treads answers. – haseakash2010 Feb 18 '22 at 20:05

1 Answers1

1

@Jeroen Mostert:

i found a final working solution from prevent exiting console app by the user.

Everybody says its not possible. But yes it possible with simple tweak.

Here we go

using C#

                RegistryKey objRegistryKey = Registry.CurrentUser.CreateSubKey(@"Console");
                objRegistryKey.SetValue("AllowAltF4Close", "0" , RegistryValueKind.DWord);

I cant post my full code here due to copyright

but this code works within my application. I tried to make a sample but that not works. But within my full application this works and Nobody can close by pressing ALT+F4.

this is a real solution for all. I don't know which part is required within my all code to make sample.

but this works.

you can just change value 0 to 1 to enable ALT+F4

CMD will not closing if you use this code. try code and launch another cmd. i dont need to launch another CMD. within my code that works for itself.

  • well, an application changing a system wide well known behavior is quite.... let's say.... annoying? You are not changing ALT+F4 effect for your application, you are changing it for every console application running on that PC. Acceptable? it depends. Not something nice to the user anyway. – Gian Paolo Feb 19 '22 at 13:44
  • @Gian Paolo : you can set its value to 1 at the end of application. That will reset original settings. after the application has exited. – haseakash2010 Feb 19 '22 at 14:02
  • 1
    This is what Raymond Chen calls "using global state to solve a local problem". These things are really only an option if you "own" the machine you're running the application on. Consider the scenario where two different applications attempt this, or even two instances of your app -- they're going to be stepping on each other's toes. The other drawback is that this registry setting is not guaranteed to keep working across releases of Windows, and in fact is likely to be removed if enough people start actively abusing it, so keep those caveats in mind. – Jeroen Mostert Feb 19 '22 at 15:02