在 C# 中获取 Windows Server 关闭原因
用户在对话框窗口中选择原因后,是否可以立即在 Windows Server 2008 中获取关机原因?对于关闭事件,我使用 SystemEvents.SessionEnding。 我想编写 Windows 服务,它将发送有关此事件的电子邮件。
或者Windows服务器中有没有其他方法可以发送有关关闭/重新启动事件的电子邮件并获取用户输入的原因?另外,我想通知有关电源更改(电线/电池)的信息,但我已经通过 Kernel32.dll > 解决了这个问题获取系统电源状态。
Is it possible to get shutdown reason in Windows Server 2008 immediately after user choose the reason in dialog window? For the shutdown event I'm using SystemEvents.SessionEnding.
I want to write windows service, which will send e-mail about this event.
Or is there any other way in windows server to send e-mails about shutdown/restart event with getting the reason entered by user? Also, I want to notify about power source change (electic line/battery), but this I have already solved by Kernel32.dll > GetSystemPowerStatus.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过检查事件日志来获取关闭原因。
我在 Windows 窗体上组装了一个快速演示,您可以使其适应您的 Windows 服务。
我已将 EventLog 组件添加到表单并正确配置它。下面的代码片段显示了在 InitializeComponent() 中为我通过设计器进行的设置生成的代码。
在事件处理程序上,您将看到如下内容:
查看事件日志以查看要过滤掉的相应 EventId。
编译器会警告您 EventID 已被弃用,并告诉您应该使用 InstanceId,但在我在这里完成的快速测试中,它没有写入我的日志文件,我认为我们已经有足够的信息来告诉您追踪。
You can get the shutdown reason inspecting the EventLog.
I assembled a quick demo on Windows Forms that you can adapt to your Windows service.
I've added a EventLog component to the Form and configured it properly. The snippet below shows the code generated in InitializeComponent() for the settings I've maid through the designer.
On the event handler, you'll have something along the following lines:
Take a look at your event log to see the appropriate EventIds to filter out.
The compiler will warn you about EventID being deprecated and telling you that you should use InstanceId, but in the quick tests I've done here, it didn't write to my log file and I think we already have enough information to put you on track.
当然有可能。
如果您想实时获取该组合框值,则需要在该进程上运行线程监视器以在该值更改时引发事件。
sure it's possible.
in case you want to get that comboBox value in real-time, you will need to run a Thread monitor on that process to raise an event when that value change.