问题描述
我有一个 Windows 服务在 PC 启动后始终运行,我将如何使用 .NET 3.5 或 WMI 进行检测,当用户或其他一些应用程序/进程在 Windows 中请求关闭或重新启动时.
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 关闭事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!