views:

28

answers:

3

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

A: 

Win32.SessionEnded event gets fired when the system is being shut down. You should attach to this in your program.

cmptrer
+1  A: 

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.

codekaizen
A: 

You're looking for the SystemEvents.SessionEnding event.

Occurs when the user is trying to log off or shut down the system.

Michael Petrotta