Windows shutdown event detection

I have a Windows service that has always been running since starting up the PC, as I would have discovered using .NET 3.5 or WMI when a user or other application / process requested a shutdown or restart on Windows.

I don’t want to stop it, just write down the time and who initiated the shutdown (the user or process is OK, you don’t need to know which process)

thanks

+3
source share
3 answers

Use the Microsoft.Win32.SystemEvents.SessionEnding event.

However, please note that you are in a Windows service, and this event will not fire for you unless you create a hidden form in your service. You can get one of them for free by enabling the option "Interaction with the desktop" in the service.

+3
source

You are looking for a SystemEvents.SessionEnding event.

Occurs when a user attempts to log off or shut down the system.

+1
source

The Win32.SessionEnded event is fired when the system shuts down. You must add this to your program.

0
source

All Articles