Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting windows shutdown event

I have a windows Service always running once PC started, how would i detect using .NET 3.5 or WMI, when the user or some other application/process has requested a shutdown or reboot in windows.

I do not want to stop it, just log the time and who initiated the shutdown (user or process is fine, don't really need to know which process)

Thank you

like image 669
Khalid Rahaman Avatar asked Sep 06 '25 06:09

Khalid Rahaman


1 Answers

Use the Microsoft.Win32.SystemEvents.SessionEnding event.

However, note that you are in a Windows Service, and this event won't fire for you unless you create a hidden form in your service. You can get one of these for free by enabling the "Interact with Desktop" option on the service.

like image 169
codekaizen Avatar answered Sep 08 '25 00:09

codekaizen