检测windows关闭事件 [英] Detecting windows shutdown event
问题描述
我有一个 Windows 服务总是在 PC 启动后运行,当用户或其他一些应用程序/进程请求在 Windows 中关闭或重新启动时,我将如何检测使用 .NET 3.5 或 WMI.
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)
谢谢
推荐答案
使用 Microsoft.Win32.SystemEvents.SessionEnding 事件.
但是,请注意您在 Windows 服务中,除非您在服务中创建隐藏表单,否则不会为您触发此事件.您可以通过启用服务上的与桌面交互"选项免费获得其中之一.
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.
这篇关于检测windows关闭事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!