Detecting windows shutdown event

我怕爱的太早我们不能终老 提交于 2019-11-27 19:27:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!