问题
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
回答1:
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.
回答2:
You're looking for the SystemEvents.SessionEnding event.
Occurs when the user is trying to log off or shut down the system.
回答3:
Win32.SessionEnded event gets fired when the system is being shut down. You should attach to this in your program.
来源:https://stackoverflow.com/questions/3173825/detecting-windows-shutdown-event